I have a table for products and a table for users who have bought products. On the products table A there is site_name which determines where the products were bought.
Table B shows the users and what they have bought.
I am using the following to show a list of products bought, by site_name, grouping the product name together.
SELECT product FROM A JOIN B ON A.prod_id = B.prod_id WHERE A.site_name = 'ebay' group by A.product
Table A for products is:
prod_id
site_name
product
Table B for users is:
user_id
prod_id
What i can’t figure out is how to get the number of products bought per line.
e.g. in table A there is
prod_id site_name product
------- --------- -------
1 ebay chair
2 amazon desk
3 ebay lamp
and on table b
user_id prod_id
------- -------
1000 1
1001 2
1002 1
1003 3
So I want to show each line where site_name is ebay and how many products were bought, order by most first like:
chair 2
lamp 1
A minor change to Michael’s query.
The above query will not return products that haven’t been bought.