MySQL Forums
Forum List  »  Newbie

Re: sum, group by, union
Posted by: Chris Stubben
Date: April 06, 2005 11:02AM

Hi,

Since products may or may not have matching rows in the delivery and sell table, you need to use left joins. Also, since left joins will return NULLs for non-matching rows, you want to convert these to zeros using the ifnull function in your SELECT query.

try this.

select p.name, sum(ifnull(d.Count, 0)), sum(ifnull(s.Count, 0))
from TblProduct p
left join TblProductDelivery d on p.Number=d.ProductNumber
left join TblProductSell s on p.Number=d.ProductNumber
group by p.name;

Chris

Options: ReplyQuote


Subject
Written By
Posted
April 06, 2005 12:47AM
Re: sum, group by, union
April 06, 2005 11:02AM
April 07, 2005 05:45AM
April 07, 2005 10:25AM
April 07, 2005 01:34PM
April 07, 2005 03:40PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.