gpt4 book ai didi

MySQL 查询不返回此查询的所有行

转载 作者:行者123 更新时间:2023-11-29 00:36:00 26 4
gpt4 key购买 nike

我有以下查询:

SELECT 
IF(COUNT(*) > 0, COUNT(*), '0') AS Found,
IF(NWLat, NWLat, '0') AS NWLat,
IF(NWLon, NWLon, '0') AS NWLon,
IF(SELat, SELat, '0') AS SELat,
IF(SELon, SELon, '0') AS SELon
FROM s
WHERE s.ci = '4'
AND s.snf = 'Ch'
GROUP BY s.sid

现在它返回匹配查询的行。但是,当没有任何匹配时,它什么也不返回。如果没有找到,我如何让它返回“Found: 0, NWLat: 0, NWLon: 0, SELat: 0, SELon: 0”?

最佳答案

最好不要在 MySQL 中这样做。 SQL与此无关。从您的前端应用程序,如果您的查询没有返回任何记录,那么您可以轻松地打印 0。

但是,如果您需要以任何方式执行此操作,您可以执行以下操作:

SELECT 
IF(COUNT(*) > 0, COUNT(*), '0') AS Found,
IF(NWLat, NWLat, '0') AS NWLat,
IF(NWLon, NWLon, '0') AS NWLon,
IF(SELat, SELat, '0') AS SELat,
IF(SELon, SELon, '0') AS SELon
FROM s
WHERE s.ci = '4'
AND s.snf = 'Ch'
GROUP BY s.sid
UNION ALL
SELECT
'0' AS Found,
'0' AS NWLat,
'0' AS NWLon,
'0' AS SELat,
'0' AS SELon
FROM s
WHERE NOT EXISTS(
SELECT
IF(COUNT(*) > 0, COUNT(*), '0') AS Found,
IF(NWLat, NWLat, '0') AS NWLat,
IF(NWLon, NWLon, '0') AS NWLon,
IF(SELat, SELat, '0') AS SELat,
IF(SELon, SELon, '0') AS SELon
FROM s
WHERE s.ci = '4'
AND s.snf = 'Ch'
GROUP BY s.sid
);

演示:

关于MySQL 查询不返回此查询的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14301487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com