我有三张桌子。 Config,capability和config_capability将前两个关联起来。
配置可以有多个功能,因此config_capability可能看起来像
id | config_id | capability_id
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
我想找到具有仅某些功能的配置。
SELECT
config.name
FROM
config
JOIN
config_capability AS cc
ON
config.id = cc.config_id
JOIN
capability
ON
cc.capability_id = capability.id
WHERE
config.node_count <= 4
AND cc.capability_id IN (
SELECT id
FROM capability
WHERE capability.name = 'cap1'
OR capability.name = 'cap2')
GROUP BY config.name
HAVING COUNT(cc.config_id) = 2
这个查询给了我配置1和配置2,但我只想找到功能1和2而不是3的配置
答案 0 :(得分:0)
您可以尝试:
HAVING COUNT(cc.config_id) = 2 and max(cc.config_id)<3
答案 1 :(得分:0)
您可以在下面重写查询,当您在联接中拥有该表时,无需使用额外的子查询
SELECT
c.name
FROM
config c
JOIN
config_capability AS cc
ON
c.id = cc.config_id
JOIN
capability ca
ON
cc.capability_id = ca.id
WHERE
config.node_count <= 4
GROUP BY c.name
HAVING sum(ca.name NOT IN('cap1','cap2')) = 0