2

I'm running the query below and it is working well.

SELECT
    stock_parts.title,
    COUNT(*) as qtyAvailable,
    SUM(shipments.item_cost) as totalValue,
    ### stock_alerts.minimum
FROM
    stock_items
INNER JOIN stock_parts ON stock_items.stock_part_id = stock_parts.id
INNER JOIN shipments ON shipments.id = stock_items.shipment_id
### INNER JOIN stock_alerts ON stock_alerts.stock_part_id = stock_items.stock_part_id
WHERE
    stock_items.status IS NULL
    AND stock_items.current_stock_id = 1
GROUP BY stock_parts.id

So if I uncomment the two commented lines (is a join to get info from another table), the query results are filtered showing only rows where stock_alerts.minimum is not null.

Why is this happening? I need all results event that column is NULL.

anderlaini
  • 1,593
  • 2
  • 24
  • 39

2 Answers2

2

What INNER JOIN does is output the row results where none of the tables have null values on that cell. In other words (from W3 Schools):

The INNER JOIN keyword selects records that have matching values in both tables

Try using FULL OUTER JOINinstead to get all the records even when one of the tables has a NULL value on it. See the explanation on W3 Schools FULL OUTER JOIN.

EDIT:

I didn't realize one of your tags is MySQL. Here you have a post that explains how to emulate a FULL OUTER JOIN in MySQL

0

Inner JOIN will ignore NULLS when you do an equal to comparison, I would suggest using IS DISTINCT FROM OPERATOR (<=>)

stock_alerts.stock_part_id <=> stock_items.stock_part_id
R Jain
  • 486
  • 3
  • 9