我需要實現一個函數,它返回安裝不屬於的所有網絡。 以下是我的表,例如,如果我的安裝ID是1,並且我需要安裝不是其中一部分的所有網絡ID,則結果將僅爲[9]
。MySQL兩個表相交
network_id | installation_id
-------------------------------
1 | 1
3 | 1
2 | 1
2 | 2
9 | 2
2 | 3
我知道這可以用連接查詢解決,但我不知道如何實現它爲同一張表。這是我到目前爲止嘗試過的。
select * from network_installations where installation_id = 1;
network_id | installation_id
-------------------------------
1 | 1
2 | 1
3 | 1
select * from network_installations where installation_id != 1;
network_id | installation_id
-------------------------------
9 | 2
2 | 2
2 | 3
兩個表的交叉將導致預期的應答,即[9]
。但是,雖然我們有union
,intersect
不在mysql中。對於找到上述兩個查詢的intersection
或者使用single query using join
來實現它的提示將非常感謝。
這是一個很好的一個。我有網絡表,查詢結果如我所料。但我想在一個選擇(在連接的幫助下)做到這一點。那可能嗎? @Gordon Linoff – DhiwaTdG
只有一個詞,「真棒」。與它鬥爭了將近幾個小時。如果你不介意,你能否向我解釋查詢的最後一行。 – DhiwaTdG
完美。謝謝。 – DhiwaTdG