NYCPHP Meetup

NYPHP.org

[nycphp-talk] MySQL - SQL Question

Hans Zaunere lists at zaunere.com
Tue Apr 22 22:21:25 EDT 2008


Hi Kristina,

> //======
> SELECT i.id, i.name, i.whatever,
> COALESCE(i.specific1, d.default1) as val1,
> COALESCE(i.specific2, d.default2) as val2
> FROM tblInstance i
> LEFT OUTER JOIN tblDefault d
> ON i.foreignKey = d.primaryKey;
> 
> COALESCE selects the first non-null value of its arguments, and the
> left outer join makes sure all records from the left table are
> returned.
> //====
> 
> Here is a nifty example of the COALESCE function that I found.  So
> basically this function is used to handle possible NULL values in a
> join ... or...?

That is a good example - an elegant if/else if/else :)

> Is this a MySQL specific thing or do other DBs use this function?

More or less.  Databases will have wonderfully standardized ways of
interpreting NULL, and even the functionality of this function, but the
concept is always the same.

This of course effectively rendering database abstraction layers in PHP
meaningless :)

H




More information about the talk mailing list