correct query for join two querys - php

I have this query for select a list
$sql="select * from buy_log where (client_id not in
(select selected_client_id from action_log where media_id=buy_log.media_id and client_id=$client_id )
and totalVisit>0 and coin_id=3 or coin_id=4 or coin_id=5)";
and this is my second select
while($row=mysqli_fetch_assoc($result)){
$media_id = $row['media_id'];
$sql2 = "select * from comment_media,comment_list where comment_list.id=comment_media.id and media_id='$media_id' order by rand() limit 2";
}
i try to use join for one query and this is my try :
SELECT S.*,WW.Comments,WW.Comments_id
FROM buy_log S
INNER JOIN
(
SELECT M.media_id,GROUP_CONCAT(W.Comment SEPARATOR '!###!') as Comments,GROUP_CONCAT(W.id SEPARATOR ',') as Comments_id
FROM comment_list W,comment_media as M
Where W.id=M.id and M.media_id='$media_id' and W.used = 0
) WW
ON S.media_id = WW.media_id
where (client_id not in
(select selected_client_id from action_log where media_id=S.media_id and client_id='1234' )
and totalVisit>0 and coin_id=3 or coin_id=4 or coin_id=5)
but here is my problem
Where W.id=M.id and M.media_id='$media_id' and W.used = 0
I don't know how can i set $media_id
any solution ?
UPDATE
Tables Info :
Tables Info : Pic

This should work:
You're extracting media id from that original query, looping through results and using the media id to assign the value $media_id in the php loop, where you perform another query.
The join seems syntactically right just need to reference the buy_log table inside the subquery.
SELECT S.*,WW.Comments,WW.Comments_id
FROM buy_log S
INNER JOIN
(
SELECT M.media_id,GROUP_CONCAT(W.Comment SEPARATOR '!###!') as Comments,GROUP_CONCAT(W.id SEPARATOR ',') as Comments_id
FROM comment_list W,comment_media as M
Where W.id=M.id and M.media_id= S.media_id and W.used = 0
) WW
ON S.media_id = WW.media_id
where (client_id not in
(select selected_client_id from action_log where media_id=S.media_id and client_id='1234' )
and totalVisit>0 and coin_id=3 or coin_id=4 or coin_id=5)

I guess that first query can be rewritten as follows... does it help?
SELECT b.media_id
FROM buy_log b
LEFT
JOIN action_log a
ON a.selected_client_id = b.client_id
AND a.media_id = b.media_id
AND a.client_id = $client_id
WHERE b.totalVisit > 0
AND b.coin_id IN(3,4,5)
AND a.selected_client_id IS NULL;

Related

multiple subquery need to change as join query

Hi all I need a solution for this question here is my query look like it works.But takes too much time because of sub queries.Give an alternate query to this query
SELECT *
FROM `room_types`
WHERE id
IN (SELECT capacity
FROM rooms
WHERE id
IN (
SELECT DISTINCT room_id
FROM `reservations`
WHERE DATE(
START ) >= '2016-01-10'
AND DATE(
END ) <= '2016-01-15'
AND STATUS = 'CheckedOut'
AND id
IN (
SELECT op_no
FROM `bills`
WHERE billed = 'Yes'
)
)
)
As of your provided query, Try this:
SELECT
`room_types`.*
FROM
`room_types`
INNER JOIN `rooms` ON (`room_types`.`id` = `rooms`.`id`)
INNER JOIN `reservations` ON (`rooms`.`id` = `reservations`.`room_id`)
INNER JOIN `bills` ON (`reservations`.`id` = `bills`.`op_no`)
WHERE
DATE(`reservations`.`START`) BETWEEN '2016-01-10' AND '2016-01-15'
AND `reservations`.`STATUS` = 'CheckedOut'
AND `bills`.`billed` = 'Yes'
Also you can index columns which will make it more faster.
So.. First try without Database Schema !
SELECT *
FROM room_types rt
JOIN rooms r ON r.capacity = rt.id
JOIN reservations resa ON r.id = resa.room_id
AND DATE(resa.start ) >= '2016-01-10'
AND DATE(resa.end ) <= '2016-01-15'
AND resa.status LIKE "CheckedOut"
JOIN bills b ON resa.id = b.resa_id AND b.billed LIKE "Yes"
Notice that "rt", "r", "resa" and "b" are aliases for your tables

Double Count Select return wrong result

Hello I am using the following query to display the count of the rows where id is the same number from first table, I have 2 more tables. If I leave only one count evertything is working fine but with double count select I am getting the same result which is wrong:
'SELECT c.*, count(s.userID) as count_consulta, count(a.userID) as count_asesoria
FROM users as c
LEFT JOIN consulta AS s ON s.userID = c.userID
LEFT JOIN asesoria AS a ON a.userID = c.userID
GROUP BY c.userID DESC'
with this I am getting:
count_consulta = 15
count_asesoria = 15
where it should be
count_consulta = 3
count_asesoria = 5
any help will be welcome. Thanks!
With the statement "any help will be welcome" I throw this out there to see if it does what you mean.
$sql="select *,
( select count( distinct s.`userID` ) from `consulta` ) as 'count_consulta',
( select count( distinct a.`userID` ) from `asesoria` ) as 'count_asesoria'
from `users` c
left outer join `consulta` s on s.`userID`=c.`userID`
left outer join `asesoria` a on a.`userID`=c.`userID`
group by c.`userID` desc;";

compare rows of two query set result in same table

I have table where I store data on basis of date.
Now I need to check that: is there any difference between data of rows with two different dates.
In a simple way you can say that I have two queries which select data from same table now I have to compare each row and column value. for example my two query are -
SELECT * FROM `national` WHERE `upload_date` = '2015-08-04' // return 106 rows
and
SELECT * FROM `national` WHERE `upload_date` = '2015-08-01' // return 106 rows
I have tried to compare this with below query but the result not seem to be correct to me, I am not satisfy with this.
Select U.* from
(SELECT t1.* FROM `national` t1 where upload_date = '2015-08-01'
union all
SELECT t2.* FROM `national` t2 WHERE `upload_date` = '2015-08-04' ) U
GROUP BY emp_id, uqi_id
HAVING COUNT(*) = 1
Can Any one please provide me correct query ?? thank you
Try this
(
SELECT t1.*
FROM
`national` t1, `national` t2
where
t1.upload_date = '2015-08-01' and t2.upload_date='2015-08-04' and
-- put your columns here that you want to compare for same DATA
-- like t1.name=t2.name and etc...
)
You can try with something like that
SELECT * FROM (SELECT * FROM `national` WHERE upload_date = '2015-08-01') a
INNER JOIN (SELECT * FROM `national` WHERE upload_date = '2015-08-04') b
ON a.emp_id = b.emp_id AND a.uqi_id = b.uqi_id
ORDER BY uqi_id

MySQL Query left join repeated records

With a Left Join i have this result.
Here the screen
http://f.cl.ly/items/373Y141r1K131d0n3f1q/Schermata%202013-04-01%20alle%2016.51.18.png
i want to show only record once time, without repeat it, but with a left join all my records are different.
what i have to do for show once all my records?
the query.
SELECT * FROM login_users
LEFT JOIN login_users_seguaci
ON login_users.user_id = login_users_seguaci.following
WHERE name LIKE ""
AND user_id != '1'
ORDER BY data DESC
SELECT x.*, y.*
FROM login_users x
LEFT JOIN
(
SELECT a.*
FROM login_users_seguaci a
INNER JOIN
(
SELECT following, MAX(DATA) max_data
FROM login_users_seguaci
GROUP BY following
) b ON a.following = b.following AND
a.DATA = b.max_date
) y ON x.user_id = y.following
// WHERE ... your condition here ...
ORDER BY t.data DESC

Limiting a left join to returning one result?

I currently have this left join as part of a query:
LEFT JOIN movies t3 ON t1.movie_id = t3.movie_id AND t3.popularity = 0
The trouble is that if there are several movies with the same name and same popularity (don't ask, it just is that way :-) ) then duplicate results are returned.
All that to say, I would like to limit the result of the left join to one.
I tried this:
LEFT JOIN
(SELECT t3.movie_name FROM movies t3 WHERE t3.popularity = 0 LIMIT 1)
ON t1.movie_id = t3.movie_id AND t3.popularity = 0
The second query dies with the error:
Every derived table must have its own alias
I know what I'm asking is slightly vague since I'm not providing the full query, but is what I'm asking generally possible?
The error is clear -- you just need to create an alias for the subquery following its closing ) and use it in your ON clause since every table, derived or real, must have its own identifier. Then, you'll need to include movie_id in the subquery's select list to be able to join on it. Since the subquery already includes WHERE popularity = 0, you don't need to include it in the join's ON clause.
LEFT JOIN (
SELECT
movie_id,
movie_name
FROM movies
WHERE popularity = 0
ORDER BY movie_name
LIMIT 1
) the_alias ON t1.movie_id = the_alias.movie_id
If you are using one of these columns in the outer SELECT, reference it via the_alias.movie_name for example.
Update after understanding the requirement better:
To get one per group to join against, you can use an aggregate MAX() or MIN() on the movie_id and group it in the subquery. No subquery LIMIT is then necessary -- you'll receive the first movie_id per name withMIN() or the last with MAX().
LEFT JOIN (
SELECT
movie_name,
MIN(movie_id) AS movie_id
FROM movies
WHERE popularity = 0
GROUP BY movie_name
) the_alias ON t1.movie_id = the_alias.movie_id
LEFT JOIN movies as m ON m.id = (
SELECT id FROM movies mm WHERE mm.movie_id = t1.movie_id
ORDER BY mm.id DESC
LIMIT 1
)
you could try to add GROUP BY t3.movie_id to the first query
Try this:
LEFT JOIN
(
SELECT t3.movie_name, t3.popularity
FROM movies t3 WHERE t3.popularity = 0 LIMIT 1
) XX
ON t1.movie_id = XX.movie_id AND XX.popularity = 0
On MySQL 5.7+ use ANY_VALUE & GROUP_BY:
SELECT t1.id,t1.movie_name, ANY_VALUE(t3.popularity) popularity
FROM t1
LEFT JOIN t3 ON (t3.movie_id=t1.movie_id AND t3.popularity=0)
GROUP BY t1.id
more info
LEFT JOIN only first row
https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
Easy solution to left join the 1 most/least recent row is using select over ON phrase
SELECT A.ID, A.Name, B.Content
FROM A
LEFT JOIN B
ON A.id = (SELECT MAX(id) FROM B WHERE id = A.id)
Where A.id is the auto-incremental primary key.
LEFT JOIN (
SELECT id,movie_name FROM movies GROUP BY id
) as m ON (
m.id = x.id
)
// Mysql
SELECT SUM(db.item_sales_nsv) as total FROM app_product_hqsales_otc as db
LEFT JOIN app_item_target_otc as it ON
db.id = (SELECT MAX(id) FROM app_item_target_otc as ot WHERE id = db.id)
and db.head_quarter = it.hqcode
AND db.aaina_item_code = it.aaina_item_code AND db.month = it.month
AND db.year = it.year
WHERE db.head_quarter = 'WIN001' AND db.month = '5' AND db.year = '2022' AND db.status = '1'

Categories