如果我有一个看起来像这样的映射表:
| entity_id | variable | | --------- | -------- | | 1 | 2010 | | 1 | M | | 1 | MA | | 2 | 2010 | | 2 | F | | 2 | MA | | 3 | 2011 | | 3 | M | | 3 | MA | | 4 | 2011 | | 4 | F | | 4 | MA | | 5 | 2010 | | 5 | M | | 5 | NY | | 6 | 2010 | | 6 | F | | 6 | NY | | 7 | 2011 | | 7 | M | | 7 | NY | | 8 | 2011 | | 8 | F | | 8 | NY |
在一组变量中查找哪个entity_ids匹配 all 的最简单查询是什么?
在集合中匹配任何变量很简单,例如:
SELECT entity_id FROM table WHERE variable IN ('2010', 'MA')
但是如果我想找到映射到'2010'和'MA'的所有entity_ids会怎么样?
理想情况下,这将是一个通用的SQL查询,但如果需要使用特殊功能/函数,则假设MySQL。
答案 0 :(得分:1)
SELECT entity_id
FROM table
WHERE variable IN ('2010', 'MA')
GROUP BY entity_id
HAVING count(*) = 2