1

I'm trying to count the number of occurrence of product_id. This is my query that's return 10 products but not according the number of occurrence of product_id.

SELECT name, category_id, product_id 
FROM product, notification 
WHERE type='product_consumed' OR type='product_rate' AND product.id = notification.product_id AND category_id="1" 
GROUP BY product_id ORDER BY product.category_id ASC LIMIT 10

I tried to COUNT the occurrenceof product_id like this, but it returns to me bad results,

SELECT name, category_id, product_id, COUNT(*) AS most_viewed 
FROM product, notification 
WHERE type='product_consumed' OR type='product_rate' AND product.id = notification.product_id AND category_id=".$cat." 
GROUP BY product_id ORDER BY product.category_id ASC, most_viewed DESC LIMIT 10

My wish is to have a sql response like this :

Category of the product | Name of product | Number of views

Thanks

4

1 回答 1

1

试试这个,你缺少一组括号

SELECT name, category_id, product_id, COUNT(*) AS most_viewed 
FROM product, notification 
WHERE (type='product_consumed' OR type='product_rate') AND product.id = notification.product_id AND category_id=".$cat." 
GROUP BY product_id 
ORDER BY product.category_id ASC, most_viewed DESC LIMIT 10
于 2012-10-31T12:06:34.010 回答