2 messages in com.mysql.lists.win32mysql Join
FromSent OnAttachments
Falk Eilenberger04 Feb 2000 08:54 
jo...@intersphere.com04 Feb 2000 09:37 
Subject:mysql Join
From:Falk Eilenberger (falk@gmx.net)
Date:02/04/2000 08:54:02 AM
List:com.mysql.lists.win32

Hi there again...here is just another (stupid) question of mine. I hope I get the same kind of fast and helpful response I got the last time, that was really great!

okay, I'm (desperately) trying to do some kind of shop system. For some kind of statistical evaluation I want to figure out how many purchases a customer has made, what their total value was, what his name is, etc. So I have all the all the customers personal stuff in a table and another table which includes all the purchases any customer has made. With a customer number I get say what customer made what purchases.

So, in order to achieve my task I'm lining the two tables like this:

SELECT customers.*, COUNT(purchases.customer_num), SUM(purchases.amount) FROM customers LEFT JOIN purchases USING(customer_num) GROUP BY customers.customer_num;

that works quite alright except in one case. That is the case if the customer is registered but has no purchases made, yet. Since mySQL creates an empty dataset for purchases.* it will count one even if the customer hasn't paid anything. The workaround using

SELECT customers.*, COUNT(purchases.customer_num), SUM(purchases.amount) FROM customers LEFT JOIN purchases USING(customer_num) WHERE customers.customer_num = 'null' GROUP BY customers.customer_num;

does not work either b/c I won't ever get a hold of all those guy who haven't made a purchase yet...

Thanks in advance.... :-)