I have the following query:
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
Right now it returns rows for matched query. However, when nothing is matched, it returns nothing. How can I make it return “Found: 0, NWLat: 0, NWLon: 0, SELat: 0, SELon: 0” if nothing is found?
It is better, not to do so in MySQL. SQL is not about that. From your front end application if your query doesn’t return any records, then you can easily print 0’s.
However, if you need to do this any way you can do this:
Demos:
IF there is no records returned, and this will give you: