2014-12-03 37 views
0

選擇了等價的,但倒元組我有2列的表,裝滿數值:如何在MySQL中

Column1  Column2 
    1    2 
    6    9 
    2    1 
    9    6 
    1    3 

元組(X,Y)等價於元組(Y,X)在我的問題的領域,我需要執行一個選擇,我只得到其中一個。意思是,我想要一個選擇查詢返回:

Column1 Column2 
    1   2 
    6   9 
    1   3 

這是在mySQL中完成這個最好的方法?

回答

1

試試這個:

select t.* 
from table t 
where column1 < column2 or 
     not exists (select 1 
        from table t2 
        where t2.column1 = t.column2 and t1.column2 = t.column1 
       ); 

如果你只是想對,即使他們不是在原來的表,那麼你可以做:

select least(column1, column2), greatest(column1, column2) 
from table t 
group by least(column1, column2), greatest(column1, column2) 
1

試試這個,這個查詢更快

Select Distinct 
     Case When Column1 >= Column2 Then Column2 else Column1 End as a, 
     Case When Column1 >= Column2 Then Column1 else Column2 End as b 
From #table