php - Count items with 3 tables -
I try to calculate the number and percentage of items from 3 tables.
Tables:
categories cat_id | Title 1 Phone 2 Tablet products prod_id | Cat_id | Title 1001 | 1 | IPhone 1002 | 1 | Nokia 1003 | 1 | Blackberry 1004 | 2 | Ipad user_products id | Prod_ID | User_id 1 | 1001 | 1 2 | 1001 | 2 3 | 1001 | 3 4 1003 | 3 5 1004 | 4 What do I still have: SELECT categories.cat_id, products.title, COUNT (products.title ) CAT categories as categories to categorize categories on categories to categorize categories on categories. Products.title by Products.cit_id GROUP Desired Results: Tablets: iPad | 1 | 100% phone: iphone | 3 | 75% Blackberry | 1 | 25%
select c.cat_id, c.title as cat_name , P .prod_id, p.title AS prod_name, COUNT (*) AS cnt, 100 * COUNT (*) / cat_total AS pct products include P INNER as products user_products p.prod_id above = up.prod_id Inner Join ( Select c.cit_id, c.title, COUNT (*) AS cat_total categories include cc cat_id = p.cat_id as CJen products user_products up up.prod_id = p.prod_id GROUP by c.cat_id) C. on ASC cat_id = p.cat_id GROUP BY p.prod_id ORDER BY cat_name, cnt DESC
Comments
Post a Comment