如何在没有任何值的字段上使用MYSQL WHERE

时间:2017-07-27 18:07:53

标签: mysql sql phpmyadmin

在脚本中,我试图获取2010Combination.cellGroom没有值的所有记录。

这是我正在使用的SQL ......

SELECT person.*, horse.*, 2010Combination.*, country.* , country.* 
FROM FEIPerson AS person JOIN 2010Combination ON person.fei_id = 2010Combination.personFEIid 
JOIN FEIHorse horse ON horse.fei_id = 2010Combination.horseFEIid 
JOIN country ON country.ISO_A3 = person.competing_for_country 
WHERE 2010Combination.cellGroom = ''
ORDER BY person.competing_for_country, ASC 

在PHPMYADMIN测试结果中没有记录,但有100 +。

这里有什么问题?

2 个答案:

答案 0 :(得分:0)

列值为空(NULL)或包含空字符串('')之间存在差异。

您的值可能是NULL,您可以使用{em> NULL-safe 的is运算符对其进行过滤。与NULL相比,大多数其他运算符返回 unknown ,这显然不是 true

WHERE 2010Combination.cellGroom IS NOT NULL

答案 1 :(得分:0)

默认情况下ORDER BYASC顺序排序,因此ASC中不需要ORDER BY。虽然我已删除了ORDER BY中的拼写错误。您需要在查询中使用IS NOT NULL来避免NULL条记录。

SELECT person.*, horse.*, 2010Combination.*, country.* , country.* 
FROM FEIPerson AS person 
JOIN 2010Combination ON person.fei_id = 2010Combination.personFEIid 
JOIN FEIHorse horse ON horse.fei_id = 2010Combination.horseFEIid 
JOIN country ON country.ISO_A3 = person.competing_for_country 
WHERE 2010Combination.cellGroom IS NOT NULL
ORDER BY person.competing_for_country