MySQL中COUNT()与WHERE子句返回0或空集问题求助
MySQL查询匹配不到预期数据(CSV导入后)
我建了数据库和phones表,把CSV文件导入后,表包含state和phone_condition等字段。之前大部分查询都正常,但下面几个简单查询死活出不来预期结果,翻了Stack Overflow试了各种方法都没用。
试过的查询和结果
- 执行这条SQL:
select state, phone_condition, count(phone_condition) as lagosUsedphones from phones where state='Lagos' and phone_condition='Used';
返回结果:
| state | phone_condition | lagosUsedphones |
|---|---|---|
| NULL | NULL | 0 |
- 执行这条:
select state, phone_condition from phones where state='lagos' and phone_condition='used';
直接返回 Empty set (0.01 sec)
- 尝试CASE语句查询:
select h.state, count( case when phone_condition='Used' and state='Lagos' then 1 else null end) as usedLagosPhones from phones h;
结果统计出的数量仍为0
- 还执行了这条:
select count(state and phone_condition) as LagosUsedPhones from phones where state='Lagos' and phone_condition='Used';
返回结果还是0
额外说明
- 直接查看表时,明明能看到
state为Lagos、phone_condition为Used的行 - 执行
select count(*) from phones where state is null and phone_condition is null;返回0,说明这两个字段没有空值 - 导入的CSV样本里,确实存在
state列值为Lagos、phone_condition列值为Used的条目
内容的提问来源于stack exchange,提问作者Kelly
相关产品推荐
相关产品推荐

