如果我添加“ AND e.first_name LIKE'%Faith%'”,则这是输出NU1605,但我想要下面查询的相同输出
SELECT COUNT(sd.id) as 'shortlisted_count',
sm.filter_details_id as id
FROM shortlisted_details sd
INNER JOIN employees e ON e.id= sd.employee_id
INNER JOIN shortlisted_master sm ON sm.id=sd.shortlisted_master_id
WHERE sd.is_disqualified=0 AND sm.filter_details_id = 136
GROUP BY sm.id
答案 0 :(得分:1)
尝试将条件sm.filter_details_id = 136
移至第二个联接的ON
子句:
SELECT
COUNT(sd.id) AS shortlisted_count,
sm.filter_details_id AS id
FROM shortlisted_details sd
INNER JOIN employees e
ON e.id = sd.employee_id AND
e.first_name LIKE '%Faith%'
INNER JOIN shortlisted_master sm
ON sm.id = sd.shortlisted_master_id AND
sm.filter_details_id = 136
WHERE
sd.is_disqualified = 0
GROUP BY
sm.id;
此更改可能解决计数问题的原因是,它防止在WHERE
子句中过早地过滤掉记录。