我有一个包含国家、州、城市和地点的 mysql 表。我只想获取那些城市或地区为空或空的国家/地区。我尝试了这个查询:
select distinct country from xyz where state != "" and ((city="" and Locality="") or (city="" and Locality!="") or (city!="" and Locality="")) order by country
基本上需要获取城市或地区值为空的所有国家/地区。此查询给出了几个在同一行中具有城市和地点的国家/地区。我究竟做错了什么?它给了我具有城市和地点两种值(value)观的国家。
我需要没有城市或地区的国家/地区列表,这意味着该国家/地区的所有城市或地区均为空或为空。如果该国家/地区的一项记录具有城市或地点值,则不需要国家/地区。
最佳答案
您正在寻找一个简单的或
:
select distinct country
from xyz
where state <> '' and
(city = '' or Locality= '')
order by country;
如果这没有返回您想要的内容,则您可能会遇到 NULL
值的问题:
select distinct country
from xyz
where state <> '' and
(city = '' or Locality= '' or city is null or Locality is null)
order by country;
或者可能不需要state
的条件。
顺便说一下,SQL 中的字符串常量应该使用单引号而不是双引号。
编辑:
如果您想要一个查询,其中给定国家/地区的所有值均为空或NULL
,则使用聚合和having
子句:
select country
from xyz
group by contry
having min(city = '' or Locality= '' or city is null or Locality is null) > 0
order by country;
关于mysql - 根据where条件获取具体记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21527162/