Skip to content
Advertisement

mySQL question on how to add more info to a column

Got this table containing info about customers, dates and their purchases values. Let’s say some of them bought something in November but nothing in December. Now, I am trying to have all the customers + all they spent and to use IFNULL() to the ones that have not bought anything in Dec. I am filtering it for Dec and all I get is the ones that purchased something in Dec, yeah… I know but I am sure there’s a way how to do that but I just can’t figure it out. Any help is more than welcome. Cheers

Advertisement

Answer

You can enumerate the customers in a subquery, then bring the december records with a left join:

This gives you one row per customer, with their total spent in December 2000 – or 0 if they did not spent during that month.

In a real-life situation, you would typically have a separate table to store the customers, that you would use instead of the subquery.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement