Solved

Mysql query syntax

Posted on 2013-11-11
6
286 Views
Last Modified: 2013-11-11
TableA
--------------------
productID,Name
1,hat
2,cup
3,pen

TableB
----------------------
orderID, productID, qtyOrdered, custID
1,1,10,1
2,1,10,1
3,3,30,1

TableC
----------------
custID, Name
1,jones

select a.productName,c.Name, sum(b.qtyOrdered) as qSum from TableA a
left join TableB b on a.productID=b.productID
left join TableC c on b.custID=c.custID
group by a.productID

returns
Hat,20,Jones
Pen,30,Jones

I want the following result
Hat,20,Jones
Cup,0,Null
Pen,30,Jones

What I want is to return a quantity of zero where no orders were placed for a product?

How do I do this ?
0
Comment
Question by:joomla
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:warddhooghe
ID: 39638492
Assuming you dont want to left join your customer's table (C) the solution could be to do the sum in a subquery inner joining B and C, then left/right join it with your products table (A)
0
 

Author Comment

by:joomla
ID: 39638502
Can you give me an example of how to construct that statement ?
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39638591
There's a function in MySQL called COALESCE that you can use to return 0 instead of NULL, but the bigger issue is with the logic in your SQL. You are grouping on ProductID, but showing the customer ID, so if 10 different customer each ordered 10 particular products, you show a SUM of 100 for the first customer - not sure that's what you want (you may need to group on the customer ID as well):

SELECT a.productName, COALESCE(SUM(b.qtyOrdered),0) AS qSum, c.Name FROM TableA a
LEFT JOIN TableB b ON a.productID=b.productID
LEFT JOIN TableC c ON b.custID=c.custID
GROUP BY a.productID, b.custID

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:joomla
ID: 39638599
thanks, I didn't know of 'coalesce' and appreciate you pointing out the need for extra grouping.

with this syntax you provided, I would assume that record in tableA without matching record in tableB would not be shown ?
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 39638617
No. Because of the LEFT join, all records from TableA will be shown (even if they have no matching record on TableB)- that's the whole point of a LEFT join.

In your original question, that's what you asked for:

What I want is to return a quantity of zero where no orders were placed for a product?
0
 

Author Closing Comment

by:joomla
ID: 39638625
Brilliant
thanks for your help
regards
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now