2017-09-01 96 views
0

我創建瞭如下的共現表。刪除表中重複的一組列

col1 col2 count 
a b 10 
b a 10 
c d 7 
d c 7 

我想保持共現行沒有這樣的重複。

col1 col2 count 
a b 10 
c d 7 

我該怎麼做?

回答

2

一個簡單的方法是:

select col1, col2, count 
from t 
where col1 < col2; 

如果你真的想改變表格,你可以這樣做:

delete t from t 
    where col1 > col2; 

這是假設所有的對列是在數據庫中。

0

當插入或選擇,做這樣的事情,而不是col1, col2

LEAST(col1, col2), GREATEST(col1, col2)