如何创建查询来对已从用户 1 发送或接收消息的用户进行分组。
SQL:
http://sqlfiddle.com/#!9/7d6a9对于 usr_msg
http://sqlfiddle.com/#!9/3ac0f对于token_msg。
我有 2 个表:token_msgs
和 user_msgs
:
我想显示已从 user1 [from_id=1 OR to_id=1] 发送/接收消息的用户 ID 列表(不重复,例如用户 2、3 和 4 只能显示一次,但按 DESC 顺序) ]。我无法为 GROUP_CONCAT
创建逻辑,并且不确定我是否真的需要加入这两个表。
谢谢。
最佳答案
假设您有一个用户列表,您可以使用 IN
或 EXISTS
:
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);
关于mysql - GROUP_CONCAT 用于对用户的多行进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38953421/