Solved

Mysql query syntax

Posted on 2013-11-11
6
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

759 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