user table
========
id(int) | name(varchar)
------------------------
1 | user1
2 | some_other_user
product table
===============
id(int) | user_id(int) | product(varchar) | status(varchar)
------------------------------------------------------------
1 | 1 | product1 | bought
2 | 1 | product3 | sold
3 | 2 | product22 | sold
4 | 2 | product2 | bought
5 | 2 | product4 | sold
6 | 1 | product5 | bought
7 | 1 | product7 | sold
I need to fetch the user’s name, how many products he has, and how many of those products have a status = sold, with a single mysql query. Any ideas?
try the following query: