我想选择每个具有相同ID的列满足条件的ID,但我无法弄清楚查询以获得该结果。 该表如下所示:
text | id
----------
text1 | 1
text2 | 1
text3 | 1
text1 | 2
text3 | 2
text2 | 3
例外结果SELECT id FROM table WHERE text IN ("text1", "text2")
id
--
1
答案 0 :(得分:2)
SELECT id
FROM `table` AS t1
INNER JOIN `table` AS t2 USING (id)
WHERE t1.text = "text1" AND t2.text = "text2"
答案 1 :(得分:0)
SELECT DISTINCT id FROM table WHERE text IN ("text1", "text2")
答案 2 :(得分:0)
再次加入同一张表
SELECT t.id from
(SELECT id FROM table WHERE text IN ("text1")) t
INNER JOIN
(SELECT id FROM table WHERE text IN ( "text2"))
on tt t.id=tt.id;