我有要求在mysql數據庫中得到一些結果的交集。但谷歌搜索後知道,沒有mysql intersect關鍵字可用。以下是我的示例表。解決mysql中相交關鍵字的問題
gene table
+------+--------+---------+
| id | symbol | test_id |
+------+--------+---------+
| -1 | A | -1 |
| 8 | A | 3 |
| 9 | G | 3 |
| -1 | A | -1 |
| -2 | B | -1 |
| -3 | C | -1 |
| 1 | A | 1 |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | B | 2 |
| 5 | C | 2 |
| 6 | D | 2 |
| 7 | E | 2 |
| 8 | A | 3 |
| 9 | G | 3 |
| 10 | F | 3 |
| 11 | C | 3 |
| 12 | C | 4 |
| 13 | G | 4 |
| 14 | F | 4 |
| 15 | M | 4 |
| 16 | N | 4 |
+------+--------+---------+
test table
+------+-------+
| id | name |
+------+-------+
| -1 | test0 |
| 3 | test3 |
| -1 | test0 |
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
+------+-------+
現在我想制定一個查詢,它會給我所提供的基因常見的測試。例如我公司將提供基因A,B,C,我應該得到以下結果:
id name id symbol
---------------------------
-1 | test0 | -1 | A
-1 | test0 | -2 | B
-1 | test0 | -3 | C
1 | test1 | 1 | A
1 | test1 | 2 | B
1 | test1 | 3 | C
我只是想通過以下方式來形成查詢,但沒有工作,得到空結果,如果我使用「或」在where子句中獲取where子句中所有基因的測試。
select distinct t.id, t.name, g.id, g.symbol from tests t
join genes g on t.id = g.test_id
where g.symbol = 'A' and g.symbol='B' and g.symbol='C';
請幫我構造查詢。
'在哪裏( 'A' g.symbol」 B','C')' – splash58
@ splash58我試過'或'而不是'和'我得到了所有這三個基因的測試,但是我只需要交叉點(這三個基因共同) –
爲什麼你不想用id -1返回?這也有基因A,B和C? – GarethD