简体   繁体   中英

mySQL query LEFT OUTER JOIN not showing null

I am trying to output all of the rows in the inventory table with the description from the parts table. Here are the tables:

Inventory Table

库存表

Parts Table

在此处输入图片说明

What I want is to have all output all of the SKUs from the inventory rows that have a specific orderNumber with the description of the SKU next to it. If the inventory SKU doesn't match a part SKU the description I want to output as NULL.

Here is my existing query:

SELECT * FROM inventory LEFT OUTER JOIN parts ON inventory.sku = parts.sku AND parts.description IS NULL WHERE orderID = $orderID

please help.

You should move the parts.description IS NULL condition to your where clause, because while joining none of the rows on the right table have null on its fields

SELECT  *
FROM    inventory
LEFT OUTER JOIN
        parts
ON      inventory.sku = parts.sku
WHERE   orderID = $orderID AND
        parts.description IS NULL 

Edit

Reading your requirements better, seems you want all the rows from the left table, regardless of them being matched or not in the right table. If that's the case, you can get rid of that condition altogether

SELECT  *
FROM    inventory
LEFT OUTER JOIN
        parts
ON      inventory.sku = parts.sku
WHERE   orderID = $orderID

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