views:

290

answers:

3

I am joining a few tables for a selection

If there isnt anything matching in the 2nd, 3rd, 4th tables I still want to pull the results as long as the first table has a match. I thought LEFT JOIN did this, but it is not.

Here is the full query:

SELECT cart_product.*, prod_drop_products.prod_drop_product_name, everlon_sheet.*, cart_product.product_id AS product_id 
FROM cart_product 
LEFT JOIN everlon_sheet ON cart_product.product_id = everlon_sheet.product_id 
LEFT JOIN prod_drop_products ON cart_product.product_id = prod_drop_products.product_id 
LEFT JOIN prod_drop ON prod_drop.prod_drop_id = prod_drop_products.prod_drop_id 
WHERE prod_drop.prod_drop_name = "Carat Weight" AND cart_product.product_brand = "everlon" 
ORDER BY cart_product.product_manufacturer_num

which pulls 316 results

Here is the query without the joins:

SELECT cart_product.* 
FROM cart_product 
WHERE cart_product.product_brand = "everlon" 
ORDER BY cart_product.product_manufacturer_num

which pulls 362 results

I have a hunch this is happening because of my WHERE prod_drop.prod_drop_name = "Carat Weight" clause in the JOIN qry. But is there a way to pull what I need in my query above but still pull everything from the first (most left, cart_product) table even if nothing matches in the other tables?

Thanks!!

+1  A: 

If there is no match on the right side prod_drop.prod_drop_name will be null

Adding OR prod_drop.prod_drop_name IS NULL should fix your problem.

Tim
Hi thanks, This does jump me up to 340. There is I know what stopping the rest but im not sure how to fix it.. they can have several prod_drop.prod_drop_name set to them, so if they have one that is set but is not CARAT WEIGHT, i still want to pull whats from cart_product, just not pull whats from prod_drop. I got bumped to 340 because its now added the ones that have no drop_prod, but the ones that do have drop_prod just not a carat weight drop_prod, i still want to select, I just dont want to pull the drop_prod, make sense? Is there a way to do that? THANKS!!!
John Isaacks
Left join on a subselect something like LEFT JOIN ( select * prod_drop WHERE prod_drop.prod_drop_name = "Carat Weight") AS prod_drop_sub ON prod_drop.prod_drop_id = prod_drop_products.prod_drop_id Then change the prod_drop columns to prod_drop_sub columns.
Tim
+1  A: 

try

WHERE prod_drop.prod_drop_name = "Carat Weight" or prod_drop.prod_drop_name is null
davek
+1  A: 

Change

WHERE prod_drop.prod_drop_name = "Carat Weight" AND cart_product.product_brand = "everlon"

to

WHERE (prod_drop.prod_drop_name = "Carat Weight" OR prod_drop.prod_drop_name IS NULL) AND cart_product.product_brand = "everlon"

The query will now also return a match if there is no matching value in prod_drop.

pmarflee
Hi, this is half of what I need. If there is a prod_drop but its not "carat weight" I still want to select whats from cart_product I just dont want to select anything from prod_drop. Also FYI they can have several drop_prods that match, but I only want to pull data from the drop_prod if the name is carat weight. If there is no drop_prod, or there is a drop_prod with a different name, then I just want to pull from cart_product. Does that make sense... would this be better to just break into multiple querys?
John Isaacks