我有一个名为' resources'
的表格Machine Host Enabled
mach1 host1 TRUE
mach2 host1 FALSE
mach3 host1 FALSE
mach4 host2 TRUE
mach5 host2 TRUE
我想获取与其关联的所有资源/机器的Enabled为True的主机列表。
我尝试了sql查询 -
select distinct Host from resources where Enabled = TRUE;
但是那个查询让我回到host1和host2作为答案,而我期待答案只是host2。任何人都可以帮我一个可以实现这个的SQL查询吗?
答案 0 :(得分:2)
试试这个:
SELECT Host
FROM resources
GROUP BY Host
HAVING COUNT(*) = COUNT(CASE WHEN Enabled = True THEN 1 END)
或:
SELECT DISTINCT Host
FROM resources AS r1
WHERE Enabled = TRUE AND
NOT EXISTS (SELECT 1
FROM resources AS r2
WHERE r1.Host = r2.Host AND r2.enabled = FALSE)
答案 1 :(得分:2)
尝试这个并让我知道。
SELECT DISTINCT(Host)
FROM Resources
WHERE Enabled = TRUE AND Host NOT IN (Select Host FROM Resources WHERE Enabled = FALSE)
答案 2 :(得分:1)
尝试
select distinct Host from resources res1 where not exist (
select 1 from resources res2 WHERE res1.host = res2.host AND Enabled = FALSE limit 1
);
答案 3 :(得分:0)
这也有效
select host from resources e
having count(e.host)=(select count(enebled) from resources
where enebled='TRUE' and host = e.host)
group by host;