组合两个列不管它们的顺序 - Mysql

时间:2018-03-14 13:17:00

标签: mysql group-by

我有这张表在我的网站上安排邮件

mail_id   from   to    mail
   1        a     b    ....
   2        a     b    ....
   3        b     a    ....
   4        a     c    ....
   5        a     c    ....

现在,我想选择一个独特的from-to或to-from对。与他们的位置无关。这是期望的结果

from  to
 a    b
 a    c

我正在使用GROUP BY来进行列,但它给了我a-b和b-a

select * from mail group by from, to

2 个答案:

答案 0 :(得分:1)

   SELECT LEAST(FROM,TO),GREATEST(FROM, TO)
     from MAIL
     GROUP BY LEAST(FROM,TO),GREATEST(FROM, TO)

可能会这样做。

答案 1 :(得分:0)

select * from mail 
where from<to
group by from, to