I am trying to get all favorite users of a particular user using this simple sub-query
select * from users u
where u.user_id in
(
select GROUP_CONCAT(f.favorite_id SEPARATOR ',') as favourites
from favourite_user f
where f.user_id in(14) group by user_id
)
When I run the subquery select GROUP_CONCAT(f.favorite_id SEPARATOR ',') as favourites from favourite_user f where f.user_id in(14) group by user_id
it gives me result 6,8,11,10,13,15,7,12
and when i run this query select * from users u where u.user_id in (6,8,11,10,13,15,7,12)
it is returning 7 rows of result
But when i run the above mentioned main query it is just giving me the 1 row (the first one) instead of 7.
Can anyone explain me what am i doing wrong. I know this can also be done with joins but I'd like to know why this approach isn't working
Thank you in advance
You don't need GROUP_CONCAT()
. Try
select *
from users
where user_id in
(
select favorite_id
from favourite_user
where user_id = 14
)
Here is SQLFiddle demo
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.