MySQL:奇怪的查询行为
我有以下三个问题: 此查询返回427MySQL:奇怪的查询行为,mysql,sql,Mysql,Sql,我有以下三个问题: 此查询返回427 select count(distinct patient_id) from patient_visits where lower(gender) = 'male'; 此查询返回272 select count(distinct patient_id) from patient_visits where lower(gender) = 'female'; 以下两个查询都返回687 select count(distinct patient_id) fro
select count(distinct patient_id) from patient_visits where lower(gender) = 'male';
此查询返回272
select count(distinct patient_id) from patient_visits where lower(gender) = 'female';
以下两个查询都返回687
select count(distinct patient_id) from patient_visits where lower(gender) in ('male', 'female');
select count(distinct patient_id) from patient_visits where lower(gender) = 'male' or lower(gender) = 'female';
为什么上次查询的结果不是427+272=699?
我在这里遗漏了什么?一些患者同时将“男性”和“女性”指定为性别。你可以找到它们:
select patient_id
from patient_visits
where lower(gender) in ('male', 'female')
group by patient_id
having count(distinct lower(gender)) = 2;
请注意,在医疗环境中,你可能要处理的性别不止两种——事实上,要多得多。非常感谢@Gordon,确实有些患者有两种性别。