tags:

views:

85

answers:

2
+3  Q: 

mysql null to 0

SELECT * from book;

how to convert NULL result to 0 from the sql ?

+5  A: 
SELECT IFNULL(pages, 0) FROM book;

if pages was the name of your column.

Peter Lang
Probably not incredibly important in most cases, but seems worth noting that Coalesce is part of the SQL Standard and IfNull is not. If portability is a concern, Coalesce is the way to go
AlexCuse
I like ifnull. coalesce sounds too pretentious.
Yada
+7  A: 

You're looking for the COALESCE keyword:

SELECT COALESCE(fieldName, 0) FROM book
Danny T.
can change fieldname to ALL ?i want to select all
nuclearmaker
@nuclearmaker: No, you have to do that for all relevant columns. Something like `SELECT COALESCE(field1, 0), COALESCE(field2, 0) FROM book;`
Peter Lang
You should never use `*` in practice, you should always list the fields you want to get.
MitMaro