我有一个mysql表,其中包含我的驱动程序日志的条目。在表中有两列:start_place和end_place。有时它可能,end_place等于start_place(我认为这听起来合乎逻辑)。
现在我不想选择表格中的条目作为tupel(x,y),而不是(y,x)。
示例:
id | start_place | end_place
-----------------------------------
0 | New York | San Francisco
-----------------------------------
1 | San Francisco | New York
id为1的行是颠倒顺序的id 0的副本,不应该是结果的一部分。
有人有想法吗?有几次我尝试过子选择或者像(x,y)!=(y,x)这样的条件,但是没有用。
答案 0 :(得分:1)
可以使用least
greatest
和group by
函数来完成此操作。
select least(start_place,end_place), greatest(start_place,end_place)
from tbl
group by least(start_place,end_place), greatest(start_place,end_place)
having count(*) = 1
要使用其他列检索此类行,请使用
select *
from tbl
where (least(start_place,end_place), greatest(start_place,end_place))
in (select least(start_place,end_place), greatest(start_place,end_place)
from tbl
group by least(start_place,end_place), greatest(start_place,end_place)
having count(*) = 1
)
答案 1 :(得分:0)
使用LEAST
,GREATEST
和DISTINCT
获取不同的对:
select distinct
least(start_place, end_place) as place1,
greatest(start_place, end_place) as place2
from mytable;