BastienSander
BastienSander

Reputation: 1828

need count to return 0 but no empty result

I'm using oracle server. Im trying to get a count of some rows in a table and when the result is 0, it always give me an empty result. I searched for an answer and find some, so I tried those different solutions, but none is working :

SELECT IFNULL(count(*), 0) as nb 
FROM tbl1 
WHERE id_tbl1='1' 
GROUP BY id_tbl2 
ORDER BY id_tbl2

SELECT IFNULL(count(id_tbl2), 0) as nb 
FROM tbl1 
WHERE id_tbl1='1' 
GROUP BY id_tbl2 
ORDER BY id_tbl2

SELECT COALESCE(id_tbl2, 0)  as nb 
FROM tbl1
WHERE id_tbl1='1' 
GROUP BY id_tbl2 
ORDER BY id_tbl2

Thanks for your help

Upvotes: 0

Views: 151

Answers (1)

John Woo
John Woo

Reputation: 263703

Remove the GROUP BY clause so you will have result even when it's empty.

SQLFiddle Demo

Upvotes: 3

Related Questions