所以这是我正在编写的应用程序的消息模块中的剥离表。
当我运行以下命令时,它按预期运行
SELECT * FROM
messages
WHERE to_user = 1或from_user = 1 GROUP BY from_user
返回
戏剧是我想要只有一个2 |的实例1或1 | 2,因为在我的应用程序中,我正在尝试根据发送消息的用户名对消息进行分组。我尝试没有OR子句,但是当1发送消息到2时,消息才会出现,直到他得到2的回复。 1和2是来自php变量的动态
答案 0 :(得分:2)
您可以使用函数来构成元组(to_user,from_user):
SELECT DISTINCT IF(from_user < to_user, from_user, to_user) AS first,
IF(from_user < to_user, to_user, from_user) AS second
FROM messages
(当然没有经过测试)。
答案 1 :(得分:1)
这有点棘手,但对我有用。
SELECT n.f, n.t
FROM
(SELECT DISTINCT m.from_user AS f, m.to_user AS t
FROM messages AS m
WHERE (m.from_user = 1
OR m.to_user =1)) AS n,
(SELECT DISTINCT m.from_user AS f, m.to_user AS t
FROM messages AS m
WHERE (m.from_user = 1
OR m.to_user =1)) AS m
WHERE n.f = m.t
AND m.f = n.t
AND n.f < m.f;
答案 2 :(得分:-1)
你可以追加
HAVING to_user < from_user