我正在尝试使用此简单的子查询来获取特定用户的所有喜欢的用户
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
)
当我运行子查询
select GROUP_CONCAT(f.favorite_id SEPARATOR ',') as favourites from favourite_user f where f.user_id in(14) group by user_id
时,它给我结果6,8,11,10,13,15,7,12
当我运行此查询
select * from users uwhere u.user_id in (6,8,11,10,13,15,7,12)
时,它将返回7行结果但是,当我运行上述主查询时,它只是给我第一行(第一行)而不是7。
谁能解释我在做什么错。我知道这也可以通过联接来完成,但是我想知道为什么这种方法不起作用
先感谢您
最佳答案
您不需要GROUP_CONCAT()
。尝试
select *
from users
where user_id in
(
select favorite_id
from favourite_user
where user_id = 14
)
这是SQLFiddle演示
关于mysql - Mysql group_concat我在做什么错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18479590/