选择包含WHERE IN子句中所有值的实体

时间:2019-03-09 11:54:41

标签: database sqlite

我想显示所有具有放置在WHERE IN中的所有值的实体,因此我使用HAVING COUNT(*)= 5;

SELECT d.semundjeName
FROM rel r JOIN
semundjet d
ON r.semundjeID = d.semundjeID
WHERE r.simptomaID IN (1, 2, 3, 4, 5)
GROUP BY d.semundjeName
HAVING COUNT(*) = 5; 

,它工作正常,但是我想做同样的事情,但是使用名称而不是id,所以我已经完成

select semundjeName
from simptomat 
left join rel
on rel.semundjeID = semundjet.semundjeID
left join semundjet
on semundjet.semundjeID = rel.semundjeID
where simptomaName IN ('Merzi','Dhimbje Koke','Gjakederdhje','Dhimbje 
Fyti','Dhimbje Kycesh')
group by semundjeName
HAVING COUNT(*) = 5; 

,它似乎不起作用。我可以完全不使用HAVING COUNT(*)来做同样的事情吗?含义是,始终显示仅在WHERE IN内部包含所有值的实体。

#semundjet - holds all disease names
######################################
semundjeID | semundjeName     
-----------------------
1            Malarja       
2            Epilepsi    
3            Depresion
4            Tuberkuloz
5            Kancer


#simptomat - holds all symptoms
#################################
simptomaID | simptomaName
------------------------- 
1            Merzi       
2            Dhimbje Koke    
3            Gjakederdhje
4            Dhimbje Fyti
5            Dhimbje Kycesh


 #rel - holds relation between diseases and symptoms
 ######################################################
 relID  | semundjeID | simptomaID
 -----------------------------
 1         1          1
 2         1          2
 3         3          1
 4         3          2
 5         3          3
 6         4          4
 7         5          5
 8         5          1
 9         5          2
 10        5          3
 11        5          4

期望的输出将是“ Kancer”,因为它是唯一包含所有症状“ simptomaID”的实体,而且我可以从第一个查询中获得该输出,而不能从第二个查询中获得。

如果有一种方法可以在不使用Haveing Count()= 5的情况下进行操作,那将是有利的。 ,但会自动返回包含IN条件中值 all 的实体作为输出*

2 个答案:

答案 0 :(得分:0)

更改:

HAVING COUNT(*) = 5

收件人:

HAVING COUNT(DISTINCT simptomaName) = 5

因为这是您要计算的,对吧?

答案 1 :(得分:0)

尝试一下

select semundjeName
from simptomat 
left join rel
on rel.semundjeID = simptomat.simptomaID
left join semundjet
on semundjet.semundjeID = rel.semundjeID
where simptomaName IN ('Merzi','Dhimbje Koke','Gjakederdhje','Dhimbje 
Fyti','Dhimbje Kycesh')
group by semundjeName
HAVING COUNT(*) = 5; 

并将所有过滤器simptomaName放入表格中,以避免=5

select semundjeName
from simptomat 
left join rel
on rel.semundjeID = simptomat.simptomaID
left join semundjet
on semundjet.semundjeID = rel.semundjeID
where simptomaName IN (select 'Merzi'
union select 'Dhimbje Koke'
union select 'Gjakederdhje'
union select 'Dhimbje Fyti'
union select 'Dhimbje Kycesh')
group by semundjeName
HAVING COUNT(*) = (select count(*) from (select 'Merzi'
union select 'Dhimbje Koke'
union select 'Gjakederdhje'
union select 'Dhimbje Fyti'
union select 'Dhimbje Kycesh') a); 

仅动态创建临时表或生成类似

的文本
select 'Merzi'
union select 'Dhimbje Koke'
union select 'Gjakederdhje'
union select 'Dhimbje Fyti'
union select 'Dhimbje Kycesh')