Skip to content
Advertisement

Join query with null values

I have this query

SELECT * FROM orders, products, suppliers 
WHERE product_id=products.id 
  AND `geleverd` = 1  
  AND supplier_id=suppliers.id

It works fine except that when the suppliers_id is NULL it is not picking those up. I need that so when a supplier is filled it should get the supplier name and when it is NULL it should also get that line because further on in the code I have if supplier_id = NULL place text

This is my suppliers database: enter image description here

And this is my orders database: enter image description here

So my query needs to show everything where geleverd is 1 no matter if supplier_id is NULL or not

Advertisement

Answer

When you also want information of suppliers when the supplier_id is null you just need to and an OR condition.

Your query will then look like as follows:

SELECT * 
FROM orders, products, suppliers 
WHERE product_id=products.id 
    AND `geleverd` = 1  
    AND (supplier_id=suppliers.id OR supplier_id IS NULL);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement