我有一个生成空集的mySQL查询。例如,
SELECT id
FROM `my_table`
WHERE type = 'old'
AND neighborhoods = 'Newport'
生成一个空集。如果查询生成一个空集,则可以让它返回:
id
---
0
谢谢。
-Laxmidi
答案 0 :(得分:0)
您可以添加查询的COUNT
部分
SELECT id, COUNT(id) as Total
FROM `my_table`
WHERE type = 'old'
AND neighborhoods = 'Newport'
这将返回至少一条Total=0
答案 1 :(得分:0)
是,
SELECT COUNT(id)
FROM `my_table`
WHERE type = 'old'
AND neighborhoods = 'Newport'
将始终返回结果集。
答案 2 :(得分:0)
如果您需要INCLUDE id,那么您可以使用LEFT OUTER来完成此任务。
SELECT a.id,
IFNULL(b.cnt,0) as cnt
FROM
(
SELECT distinct id, 'myjoin' as myjoin
FROM `my_table`
WHERE id = [what you are looking for]
) a LEFT OUTER JOIN
(
SELECT COUNT(id) as cnt
FROM `my_table`
WHERE type = 'old'
AND neighborhoods = 'Newport'
) b ON a.myjoin=b.myjoin
第一个查询返回你想要的id然后加入(LEFT OUTER)到一个固定值(在这种情况下称为'myjoin'),如果后一个查询返回空,将返回0。