使用以下查询时,我得到的结果不正确。在处理子句和HAVING中的多个where时,如何解决这个问题。
SELECT *
FROM `otc_employee_qualifications`
WHERE `emp_qualifctn_type`
IN (
'26', '27'
)
AND `qualification_value`
IN (
'62', '64','65'
)
AND `qualification_mark` >= '10'
GROUP BY `employee_id`
HAVING COUNT( DISTINCT `emp_qualifctn_type` ) = '2'
AND COUNT( DISTINCT `qualification_value` ) = '3'
LIMIT 0 , 30
PHPMYADMIN
在这里,我需要获得满足emp_qualifctn_type(27和26)以及(' 62',' 64',' 65&}的所有资格值的员工ID #39)
答案 0 :(得分:1)
我在查询中发现了错误。我应该在查询中使用'OR'来组合HAVING子句中的多个案例。
SELECT *
FROM `otc_employee_qualifications`
WHERE `emp_qualifctn_type` IN ('26', '27')
AND `qualification_value` IN ('62', '64','65')
AND `qualification_mark` >= '10'
GROUP BY `employee_id`
HAVING COUNT( DISTINCT `emp_qualifctn_type` ) = '2'
OR COUNT( DISTINCT `qualification_value` ) = '3'
这里的结果是Null,这是真的。
答案 1 :(得分:0)
更新评论I want to get all employee_ids say, who have both emp_qualifctn_type (27 , 29) and both qualification_value (62,50). Here the result would be 2.
。检查这个=>
SELECT *
FROM `otc_employee_qualifications`
WHERE `emp_qualifctn_type`
IN (27, 29)
AND `qualification_value`
IN (62, 50)
AND `qualification_mark` >= 10
GROUP BY `employee_id`
HAVING count(DISTINCT emp_qualifctn_type) >= 2
LIMIT 0 , 30
答案 2 :(得分:0)
根据上述数据,没有员工满足您的评论的所有值我想让所有employee_ids说明,谁同时拥有emp_qualifctn_type(27,29)和qualified_value(62,50)
例如对于2,第一行在emp_qualifctn_type和qualification_value上都匹配,但第二行仅在emp_qualifctn_type上匹配,而第三行仅在qualified_value上匹配。所以2对所有人都没有匹配。
为了获得你想要的东西,我认为你需要匹配一个或另一个而不是AND,然后你可以检查计数: -
SELECT employee_id
FROM `otc_employee_qualifications`
WHERE (`emp_qualifctn_type` IN (27, 29)
OR `qualification_value` IN (50, 62))
AND `qualification_mark` >= '10'
GROUP BY `employee_id`
HAVING COUNT( DISTINCT `emp_qualifctn_type` ) = 2
AND COUNT( DISTINCT `qualification_value` ) = 2