Is it possible to make a join in mysql so that the values from the right table are returned if and only if the corresponding values in the left table don’t exist?
For example
SELECT * FROM
(SELECT userid,status FROM date_status WHERE date='20-03-2012') ds
JOIN
(SELECT userid,status FROM default_weekday_status WHERE day='tuesday') dws
ON
ds.userid=dws.userid
I would like this to return date_status.status if the row in date_status exists, but default_weekday_status.status otherwise.
(Note that 20-03-2012 is a Tuesday and no, tuesday wouldn’t be stored as a string 😉 )
The easiest way to do this is with the COALESCE function. I will return the first not-null value in the list of parameters.
The sub-queries have been removed, as they aren’t very efficient.
MySQL – COALESCE