我有一个包含数千个数据的表位置
我的SQL查询是
select *
from location_table
where country = "PH"
and (province = "NCR" OR province = "")
and (city = "makati" or city="") limit 10
location_table
ID | country | province | city
1 | PH | NCR | makati
2 | PH | NCR | makati
3 | PH | NCR | makati
4 | PH | NCR | makati
5 | PH | NCR | marikina
6 | PH | NCR |
7 | PH | |
8 | US | |
这是正确的。这是否可以确保我的输出将是这样的
ID | country | province | city
1 | PH | NCR | makati
2 | PH | NCR | makati
3 | PH | NCR | makati
4 | PH | NCR | makati
6 | PH | NCR |
7 | PH | |
我需要确保我的查询是准确的。我需要改进我的查询吗?我需要优先考虑城市和省的价值。如果它有一个省。或者获取与城市相同的null值。
例如,city = makati 我需要优先考虑所有城市的“makati”价值。如果在ID 3000上怎么办?那里有一个makati。我需要首先使用makati获取所有城市,而不是使用null值的城市。与省相同。
答案 0 :(得分:2)
使用ORDER BY
将非空值放在第一位:
select *
from location_table
where country = "PH"
and (province = "NCR" OR province = "")
and (city = "makati" or city="")
order by province = "", city = ""
当列为空时, columnname = ""
将为1
,否则为0
。
答案 1 :(得分:0)
select *
from location_table
where country = "PH"
and (province = "NCR" OR province = "")
and (city = "makati" or city="")
order by province, city
Order By将解决您的问题。
答案 2 :(得分:0)
要检查null,请始终使用'为空'条款。 find_in_set也可以帮到你