Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

2 x SUM(IF.. and 2x LEFT JOIN

 
   Database Help (Home) -> mySQL RSS
Next:  can any one know the simple way of moving a comma..  
Author Message
Quarco

External


Since: Aug 13, 2004
Posts: 1



(Msg. 1) Posted: Thu Feb 17, 2005 7:40 am
Post subject: 2 x SUM(IF.. and 2x LEFT JOIN
Archived from groups: mailing>database>mysql (more info?)

Hi,

Suppose I have a query like:

SELECT
products.name AS product,
SUM(IF(stock.invoice=0,1,0)) AS in_stock,
SUM(IF(shopcart.status=1,1,0)) AS reserved
FROM products
LEFT JOIN stock ON products.id=stock.product_id
LEFT JOIN shopcart ON products.id=shopcart.product_id
GROUP BY products.id

I get wrong results..
E.g. I now have 1 item 'reserved' and 5 in stock; I get 5 reserverd and 5 in
stock..
Is there a workaround for this problem??


Thanx in advance,

Marco

 >> Stay informed about: 2 x SUM(IF.. and 2x LEFT JOIN 
Back to top
Login to vote
Bill Karwin1

External


Since: Jun 17, 2004
Posts: 42



(Msg. 2) Posted: Fri Feb 18, 2005 3:30 pm
Post subject: Re: 2 x SUM(IF.. and 2x LEFT JOIN [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Quarco wrote:
 > Hi,
 >
 > Suppose I have a query like:
 >
 > SELECT
 > products.name AS product,
 > SUM(IF(stock.invoice=0,1,0)) AS in_stock,
 > SUM(IF(shopcart.status=1,1,0)) AS reserved
 > FROM products
 > LEFT JOIN stock ON products.id=stock.product_id
 > LEFT JOIN shopcart ON products.id=shopcart.product_id
 > GROUP BY products.id
 >
 > I get wrong results..
 > E.g. I now have 1 item 'reserved' and 5 in stock; I get 5 reserverd and 5 in
 > stock..
 > Is there a workaround for this problem??

An outer join doesn't return just zero or 1 match; it returns N matches,
which result in N rows. So for example if you have 5 in stock for
product id 327, then you get 5 rows corresponding to product id 327.
Thus those 5 rows _each_ match the one item reserved.

Try this query and see what I mean:

SELECT
products.name AS product,
IF(stock.invoice=0,1,0) AS in_stock,
IF(shopcart.status=1,1,0) AS reserved
FROM products
LEFT JOIN stock ON products.id=stock.product_id
LEFT JOIN shopcart ON products.id=shopcart.product_id
ORDER BY products.id;

It works both ways too. If you have 2 reserved and 5 in_stock, then you
get 2*5 rows returned, and the sum for both in_stock and reserved
becomes 10.

I can suggest two alternatives:

1. Do each sum calculation in separate queries:

SELECT
products.name AS product,
SUM(IF(stock.invoice=0,1,0)) AS in_stock,
FROM products
LEFT JOIN stock ON products.id=stock.product_id
GROUP BY products.id;
SELECT
products.name AS product,
IF(shopcart.status=1,1,0) AS reserved
FROM products
LEFT JOIN shopcart ON products.id=shopcart.product_id
GROUP BY products.id;

2. Do the summation in subqueries (requires MySQL 4.1):

SELECT
p.name AS product,
(SELECT SUM(IF(s.invoice=0,1,0)) FROM stock AS s WHERE s.product_id
= p.id) AS in_stock,
(SELECT SUM(IF(h.status=1,1,0)) FROM shopcart AS h WHERE
h.product_id = p.id) AS reserved
FROM products AS p
ORDER BY products.id

Regards,
Bill K.

 >> Stay informed about: 2 x SUM(IF.. and 2x LEFT JOIN 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Question about UPDATE and LEFT JOIN - Hi, I hope this is the right place to post, but, I am having a problem with an UPDATE command and a LEFT JOIN, I am using something like: UPDATE table_a LEFT JOIN table_b ON table_a.field1=table_b.field1 SET table_b.field6='1' WHERE table_a.field2='1'; ...

Join and limit the result question - Hi guys! I have a challenge that I hope you can help me with. I have three tables and I have exported them with some sample data and uploaded them here: http://server.arton.no/filesharing/SQL.zip Here is how the tables looks like:..

Best way to issue hundreds of inserts/updates??? - Using mysql 4.0.23- What is the best way to execute several (hundreds of) inserts and updates? Rather than issuing tons of individual inserts and updates, can I send the strings to a text file and then have mysql do them all?? IE : query.txt insert..

MySQL freezes, brings XP machine to a grinding halt - I've been using MySQL for a while for fairly light database development on my XP machine. Currently, I am just starting a new project and have experienced some big problems with MySQL today both on my office machine and at home where running a particular...

login as user 'root' but do not have root privlages and my.. - Hi gang: I'm experiencing a problem with MySQL -- I updated MySQL from version 4.1.0 to 4.1.10 and now when I login as root it doesn't show all the databases I should have access to, nor it doesn't recognize me being logged in as root (via..
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]