繁体   English   中英

GROUP_CONCAT用于将用户的多行分组

[英]GROUP_CONCAT for grouping multiple rows for a user

如何创建查询以对从用户1发送或接收消息的用户进行分组。

SQL: http ://sqlfiddle.com/#!9 / 7d6a9 for usr_msg
http://sqlfiddle.com/#!9/3ac0f获取token_msg。

我有2个表: token_msgsuser_msgs

我的桌子的图片:

我想显示已从user1 [from_id = 1 OR to_id = 1]发送/接收消息的用户ID列表(非重复,如用户2、3和4应该只可见一次,但按DESC顺序显示)。 我无法为GROUP_CONCAT创建逻辑,也不确定是否真的需要加入这2个表。

在此处输入图片说明

谢谢。

假设您具有用户列表,则可以使用INEXISTS

select u.*
from users u
where u.id in (select from_id from user_msgs where to_id = 1) or
      u.id in (select to_id from user_msgs where from_id = 1) or
      u.id in (select from_id from token_msgs where to_id = 1) or
      u.id in (select to_id from token_msgs where from_id = 1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM