2017-02-19 66 views
2

我有像這樣的表:MYSQL - WHERE在兩列中的值

id | node_source | node_destination | path 
1 |  1  |  0   | {"coordinates": [[-6.27400693507... 
2 |  0  |  1   | {"coordinates": [[-6.24568104953... 
3 |  1  |  2   | {"coordinates": [[-6.24568104953... 
4 |  2  |  1   | {"coordinates": [[-6.27230059993... 

我想行1和行2,行3和行4

之間比較node_source和node_destionation的值如果node_source在第1行== node_destination在第2行AND node_destination在第1行== node_source在第2行THEN只顯示第一行(id = 1)

if row_strings == node_destination in row 4 AND node_destination in第3行==第4行中的node_source THEN僅顯示第三行(id = 3)

最終的輸出是這樣的:

id | node_source | node_destination | path 
1 |  1  |  0   | {"coordinates": [[-6.27400693507... 
3 |  1  |  2   | {"coordinates": [[-6.24568104953... 

這裏我的代碼(但不工作):

SELECT * FROM graph t1 
    WHERE NOT EXISTS(SELECT * FROM graph t2 
         WHERE t2.node_source = t1.node_destination AND t2.node_destination = t1.node_source 
        ) 

幫助。 謝謝。

回答

1

我想你想要這樣的:

SELECT g.* 
FROM graph g 
WHERE g.node_source <= g.node_destination 
UNION ALL 
SELECT g.* 
FROM graph g 
WHERE g.node_source > g.node_destination AND 
     NOT EXISTS (SELECT 1 
        FROM graph g2 
        WHERE g2.node_source = g.node_destination AND g2.node_destination = g.node_source 
       ); 

這將選擇每對中的一個邊緣。

0

您可以使用自聯接和ID和id + 1之間的比較,

但是請注意,這將在2和3之間等

select a.id, 
     a.node_source, 
     a.node_destination, 
     a.path 
from graph a 
inner join graph b on a.id=b.id+1 
where (a.node_source<> b.node_destination 
or b.node_source<> a.node_destination) 
+0

也比較但是,如果我改變第二行與: 'id | node_source | node_destination | 2 | 4 | 1 \t \t |' 輸出將會不同: 'id | node_source | node_destination |路徑 2 | 4 | 1 | {「coordinates」:[[-6.24568104953 ... 3 | 1 | 2 | {「coordinates」:[[-6.24568104953 ...' – ahm

+0

據我瞭解,在這種情況下,你應該得到行1,2和3,這就是查詢將帶來什麼 – user3600910