Solved

Mysql query syntax

Posted on 2013-11-11
6
285 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 42

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 42

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to dynamically set the form action using jQuery.

762 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

21 Experts available now in Live!

Get 1:1 Help Now