I have 3 tables
products table productid productname --------- ----------- 1 product 1 2 product 2 5 product 3 10 product 4 11 product 5 12 product 6 accounts_products table id productid accountid -- --------- --------- 1 1 accountid 1 2 10 accountid 2 3 2 accountid3 leads_products table id productid leadid -- --------- --------- 1 1 leadid 1 2 5 leadid 2 3 2 leadid 3 I am trying to count how many total products are in leads_products and accounts_products tables based on the same productid's.
Expected result Product ID Product Name Total ----------- ------------ -------- 1 product 1 2 2 product 2 2 5 product 3 1 10 product 4 1 I tried so far
SELECT p.productid as 'Product ID', p.productname as 'Product Name', COUNT(*) as 'Total' FROM products p INNER JOIN leads_products l ON (l.productid=p.productid) INNER JOIN accounts_products a ON (a.productid=p.productid) GROUP BY p.productname,p.productid Above query counts and display higher number than expected.
I hope it makes sense.