为什么此查询会返回不需要的行

时间:2014-02-20 16:38:59

标签: mysql sql

此查询返回latitude = 0longitude = 0

的行
SELECT `id`, 
              `first_name`, 
              `last_name`, 
              `address`, 
              `latitude`, 
              `longitude` 
              FROM `members` 
              WHERE (`temp` != 'on' OR `temp` IS NULL) 
              AND (`backup` != 'on' OR `backup` IS NULL) 
              AND (`latitude` IS NOT NULL OR `latitude` != 0 OR `latitude` != '') 
              AND (`longitude` != 0 OR `longitude` != '' OR `longitude` IS NOT NULL) 
              AND `address` != '' 
              AND `type_id` = 1
              AND `first_name` != ''

查询返回lat / long有0值的行 - Datatype = VARCHAR

1 个答案:

答案 0 :(得分:4)

您需要and,而不是or

          WHERE (`temp` != 'on' OR `temp` IS NULL) 
          AND (`backup` != 'on' OR `backup` IS NULL) 
          AND (`latitude` IS NOT NULL and `latitude` != 0 and `latitude` != '') 
          AND (`longitude` != 0 and `longitude` != ''and `longitude` IS NOT NULL) 
          AND `address` != '' 
          AND `type_id` = 1
          AND `first_name` != ''

Latitude可能为0(未通过该条件),但不是NULL - 传递该条件。