views:

437

answers:

3

I need to put a Case in the Select to check if the Data I'm adding to my view is NULL, in which case I want it to just enter a zero, or not.

+1  A: 

When creating your table just add NOT NULL to the column description, e.g.

CREATE TABLE ( ID INT NOT NULL default '0' );

Then if no data is given for the field it is set to the default value of 0 which will be retrieved when you run a SELECT query.

Jacob Wyke
this doesn't apply if, for example, the NULL is the result of a LEFT OUTER JOIN.
longneck
A: 

You mean something like this?

SELECT IF(`field` IS NULL, 0, `field`)...

There's also "IFNULL()":

SELECT IFNULL(`field`, 0)...
Adam Bradley
this is a classic case of misusing IF() and IFNULL() where the more generally accepted COALESCE() is a better idea.
longneck
+2  A: 
select coalesce(field, 0) as 'field' from v;

(doc)

joeslice