下面是我的桌子:
我想基于sender
和Reciver
的值来获取结果,并以没有多余结果的方式对它们进行分组。
示例:
发件人= 2
接收者= 1
reciver = 2
sender = 1
在上面的示例中,不应同时显示两个结果,而应仅显示一个结果。
我正在使用mysql,下面提供了我正在使用的代码,该代码给出了多个结果,如图所示。
SELECT Mid,sender,Reciver
FROM `tbl_messages`
WHERE sender=1 or Reciver = 1
GROUP by Reciver,sender or sender,Reciver;
答案 0 :(得分:0)
尝试一下:
SELECT newSender, newReceiver/*some aggregate functions or access other comlumns*/ FROM (
SELECT Mid,/* add more columns if needed*/
case when sender < reciver then sender else receiver end newSender,
case when sender > reciver then sender else receiver end newReceiver
FROM `tbl_messages`
WHERE sender = 1 or Reciver = 1
) a GROUP by newSender, newReceiver;