vican Posted April 14, 2008 Share Posted April 14, 2008 Fairly new to PHP/Mysql, help please... Say I have 2 tables Table A: Customer ID | Name 1 | Adam 2 | Bravo 3 | Charlie 4 | Dennis Table B: Sales ID Year Amount 1 2007 10 1 2007 20 1 2008 30 2 2008 40 2 2008 50 2 2008 60 3 2007 70 3 2007 80 4 2008 100 and I need to list all customers and their sales amount in 2008(even if they don't have any sales in 2008). I had the following code which only returns 2008 results. Select Customer.ID, Customer.Name, sum(Sales.Amount ) from Customer Left join Sales on customer.id= sales.id Where sales.year = 2008 group by customer.name I am stuck. Any help is greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/101125-help-with-php-code-left-join/ Share on other sites More sharing options...
Barand Posted April 14, 2008 Share Posted April 14, 2008 Select Customer.ID, Customer.Name, sum(Sales.Amount ) from Customer Left join Sales on customer.id= sales.id AND sales.year = 2008 group by customer.name Link to comment https://forums.phpfreaks.com/topic/101125-help-with-php-code-left-join/#findComment-517183 Share on other sites More sharing options...
vican Posted April 14, 2008 Author Share Posted April 14, 2008 ouch....you made it look easy, barand It works and Thanks a million Link to comment https://forums.phpfreaks.com/topic/101125-help-with-php-code-left-join/#findComment-517189 Share on other sites More sharing options...
Barand Posted April 14, 2008 Share Posted April 14, 2008 Rule: When LEFT JOINING there may not be a record in the RIGHT table so if the WHERE condition is on that table, put it in the JOIN condition. Link to comment https://forums.phpfreaks.com/topic/101125-help-with-php-code-left-join/#findComment-517194 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.