Solved

Mysql query syntax

Posted on 2013-11-11
6
289 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

821 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