我有下表,并尝试根据ID对行进行分组。我希望访问返回所有类型包含相同id(a19)的S和A的情况,但不返回S或A彼此不存在的返回实例(a22& a33)。
最简单的方法是什么?
+---+----+--------------+
|id |type| text |
+---+----+--------------+
a19 S S1
a19 A A1
a19 A A1
a22 Y A1
a33 S S1
+---+----+--------------+
答案 0 :(得分:3)
这是一个返回包含A和S类型的所有ID的查询:
SELECT subQuery.id
FROM (SELECT group_table.id
FROM group_table
WHERE (((group_table.type) IN ("A","S")))
GROUP BY group_table.id, group_table.type) AS subQuery
GROUP BY subQuery.id
HAVING Count(subQuery.id) = 2
你可以通过用“SELECT FilterType FROM FilterTable”替换“... IN(”A“,”S“)......”来使其有些灵活。“ strong>和更改“... Count(subQuery.id)= 2 ...”到例如“... Count(subQuery.id)= SELECT COUNT(FilterType)FROM FilterTable ...”