MySQL Left Join, SELECT a field either from one or the other if null?

Just add a COALESCE : SELECT `tblSettings`. `id`, `tblSettings`. `setting_name`, COALESCE(`tblAgencySettings`.

`setting_value`, `tblSettings`. `setting_value`) FROM `tblSettings` LEFT JOIN `tblAgencySettings` ON `tblSettings`. `id` = `tblAgencySettings`.

`setting_id` AND `tblAgencySettings`. `agency_id` = '1' WHERE `tblSettings`. `changeable` = '1 The COALESCE function returns the first non-NULL value you give it so this: COALESCE(`tblAgencySettings`.

`setting_value`, `tblSettings`. `setting_value`) Will be tblAgencySettings. Setting_value if that's not NULL and tblSettings.

Setting_value if tblAgencySettings. Setting_value is NULL If tblAgencySettings. Setting_value can also be zero and you want to ignore that as well as NULL, then you could use this instead of the COALESCE above: COALESCE( IF(`tblAgencySettings`.

`setting_value` = 0, NULL, `tblAgencySettings`. `setting_value`), `tblSettings`. `setting_value` ) The IF returns the second argument if the first is true and the third if the first argument is false so the above use converts zero to NULL.

Or, you could go all the way to a CASE statement: case when `tblAgencySettings`. `setting_value` = 0 then `tblSettings`. `setting_value` when `tblAgencySettings`.

`setting_value` IS NULL then `tblSettings`. `setting_value` else `tblSettings`. `setting_value` end.

Just add a COALESCE: SELECT `tblSettings`. `id`, `tblSettings`. `setting_name`, COALESCE(`tblAgencySettings`.

`setting_value`, `tblSettings`. `setting_value`) FROM `tblSettings` LEFT JOIN `tblAgencySettings` ON `tblSettings`. `id` = `tblAgencySettings`.

`setting_id` AND `tblAgencySettings`. `agency_id` = '1' WHERE `tblSettings`. `changeable` = '1' The COALESCE function returns the first non-NULL value you give it so this: COALESCE(`tblAgencySettings`.

`setting_value`, `tblSettings`. `setting_value`) Will be tblAgencySettings. Setting_value if that's not NULL and tblSettings.

Setting_value if tblAgencySettings. Setting_value is NULL. If tblAgencySettings.

Setting_value can also be zero and you want to ignore that as well as NULL, then you could use this instead of the COALESCE above: COALESCE( IF(`tblAgencySettings`. `setting_value` = 0, NULL, `tblAgencySettings`. `setting_value`), `tblSettings`.

`setting_value` ) The IF returns the second argument if the first is true and the third if the first argument is false so the above use converts zero to NULL. Or, you could go all the way to a CASE statement: case when `tblAgencySettings`. `setting_value` = 0 then `tblSettings`.

`setting_value` when `tblAgencySettings`. `setting_value` IS NULL then `tblSettings`. `setting_value` else `tblSettings`.

`setting_value` end.

Nice. It worked perfectly I just added as setting value after your line of code. – M.

Of CA Aug 8 at 4:21 slight issue I just noticed. I failed to mention this. If tblAgencySettings.

Setting_valuedoes have a value. But changeable is 0 then just select tblSettings. Setting_value – M.

Of CA Aug 8 at 4:26 @M. Of CA: I added an update with some options for dealing with the "0 or NULL" issue. – mu is too short Aug 8 at 4:33.

Change your SQL Statement to this: SELECT `tblSettings`. `id`, `tblSettings`. `setting_name`, CASE WHEN `tblSettings`.

`setting_value` IS NULL THEN `tblAgencySettings`. `setting_value` ELSE `tblSettings`. `setting_value` END AS `setting_value` FROM `tblSettings` LEFT JOIN `tblAgencySettings` ON `tblSettings`.

`id` = `tblAgencySettings`. `setting_id` AND `tblAgencySettings`. `agency_id` = '1' WHERE `tblSettings`.

`changeable` = '1' Here's a link to MYSQL CASE Statement for your reference.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions