I have three tables: Clinic
, Stock
and StockLog
.
I need to get all rows where Stock.stock < 5
. I need to also show if an order has been placed and what amount it is; which is found in the table Stocklog
.
The issue is that a user can set his stock level in the Stock
table without placing an order which would go to Stocklog
.
I need a query that can : return the rows in the Stock
table and get the related order amounts in the Stocklog
table. If no order has been placed in StockLog
, then set amount to order amount to zero.
I have tried :
SELECT
Clinic.Name,
Stock.NameOfMedication, Stock.Stock,
StockLog.OrderAmount
FROM
Clinic
JOIN
Stock ON Stock.ClinicID = Clinic.ClinicID
JOIN
StockLog ON StockLog.StockID = Stock.StockID
WHERE
Stock.Stock <= 5
The issue with my query is that I lose rows which are not found in StockLog
.
Any help on how to write this.
Thank you.