Hi there!

I have three tables:
1. Products
     - id
     - name
2. OutOrders (orders from customers to us)
     - id
     - productid
     - quantity
3. InOrders (our ourders to the traders)
     - id
     - productid
     - quantity

When I try to find out the current amount of products in our stock, and ordered quantities I use this query:

SELECT p.id, SUM(out.quantity) ordered_out, SUM(in.quantity) ordered_in FROM products p LEFT JOIN outorders out ON (out.productid = p.id) LEFT JOIN inorders (in.productid = p.id);

Even I have only one "OutOrder" for certain product, I get ordered_out value as 2. ordered_in value is anyway correct. If I remove all things related to "InOrders" I will get correct result to ordered_out.

Have I missed something? Isn't LEFT JOIN the correct query to join tables to the "main" table (in this case products)?

Thanks!

Ville
--
.....................................................
Ville Mattila                        gsm 040 849 7506
Pilotmedia                          fax (03) 458 9080
Tapiolankatu 11           [EMAIL PROTECTED]
39500 Ikaalinen                     www.pilotmedia.fi
.....................................................

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



Reply via email to