简体   繁体   中英

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: 在此处输入图片说明

And this is my orders database: 在此处输入图片说明

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM