mo Posted May 15, 2009 Share Posted May 15, 2009 I have a query in which I join 3 tables 2 tables have matching data and the third table is currently empty (no rows at all). I want my query to still return results from the successful join even though the 3 table is empty. Tables order_item and store have matching entries and order_fax_status is empty. SELECT DISTINCT a.`store_id`, a.`store_name`, b.`fax` FROM `order_item` AS a INNER JOIN `store` AS b ON b.`store_id` = a.`store_id` LEFT JOIN `order_fax_status` AS c ON c.`store_id` = a.`store_id` WHERE a.`od_id` = 3 AND a.`store_id` <> 1000 AND c.`fax_status` != 'SUCCESS' If I remove "AND c.`fax_status` != 'SUCCESS'", the query works. Quote Link to comment Share on other sites More sharing options...
fenway Posted May 15, 2009 Share Posted May 15, 2009 You can't check a field from a left joined table if it doesn't match, since it will be NULLed out. Move this to the ON clause: SELECT DISTINCT a.`store_id`, a.`store_name`, b.`fax` FROM `order_item` AS a INNER JOIN `store` AS b ON ( b.`store_id` = a.`store_id` ) LEFT JOIN `order_fax_status` AS c ON (c.`store_id` = a.`store_id` AND c.`fax_status` != 'SUCCESS' ) WHERE a.`od_id` = 3 AND a.`store_id` <> 1000 Quote Link to comment Share on other sites More sharing options...
mo Posted May 15, 2009 Author Share Posted May 15, 2009 You can't check a field from a left joined table if it doesn't match, since it will be NULLed out. Move this to the ON clause: SELECT DISTINCT a.`store_id`, a.`store_name`, b.`fax` FROM `order_item` AS a INNER JOIN `store` AS b ON ( b.`store_id` = a.`store_id` ) LEFT JOIN `order_fax_status` AS c ON (c.`store_id` = a.`store_id` AND c.`fax_status` != 'SUCCESS' ) WHERE a.`od_id` = 3 AND a.`store_id` <> 1000 Perfect! Works like intended, thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.