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

1
  • can you please post some example data of your actual and expected result? Commented Oct 31, 2012 at 12:06

1 Answer 1

1

Try this, you are missing a set of brackets

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.