LEFT JOIN is the way to go, but you need to make the invoice ID part of the JOIN condition rather than putting it in the WHERE clause.

  SELECT i.IDItems, i.Name, IFNULL(inv.Qty, 0) AS Qty
  FROM Items i LEFT JOIN Invoice inv ON i.IDItems = inv.IDItems
                                        AND inv.IDInvoice = 1001;

Michael

Cedric wrote:

Hi,

I'm quite new to mysql and I need to join two tables:

Items
IDItems Name
1       Orange
2       Apple
3       Bread
4       Milk
...

Invoice
IDInvoice IDItems Qty
1001      1       10
1001      3       2
1002      2       5
...

I need to get all items and for items included in invoice (IDInvoice) the qty

Results for IDInvoice = 1001
IDItem Name    Qty
1      Orange  10
2      Apple   0
3      Bread   2
4      Milk    0

Did somebody have an idea? I tried with LEFT JOIN / RIGHT JOIN / INNER JOIN...
with no success

Thanks for any help
Cedric



--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]



Reply via email to