1internet Posted April 25, 2013 Share Posted April 25, 2013 What I am trying to do is just provide a report showing the most popularly sold products. I am trying to create a sql result that will show the `product name`, along with the `quantity` of that product sold, and order it by quantity. So essentially I want to count how many of each product id is in the result, and maybe create this count as an alias to go with the product id. From there I display the product name (associated with the id) with the quantity. The table has these fields: orders_products_id - the id of the row products_id - the id of the product, this is what we want to count products_name - the name of the product, this is what we want to display in the results and to add extra confusion there is also a quantity column orders_products_id | products_id | products_name | quantity 1 | 7 | apple | 1 2 | 4 | orange | 3 3 | 11 | grapes | 6 4 | 7 | apple | 3 5 | 4 | orange | 1 6 | 7 | apple | 1 7 | 17 | pear | 2 So the outcome I would be seeking from this would be grapes 6 apple 5 orange 4 pear 2 So I just need to know what the sql query would be. Link to comment https://forums.phpfreaks.com/topic/277281-most-popular-id-in-a-row-in-a-table/ Share on other sites More sharing options...
jugesh Posted April 25, 2013 Share Posted April 25, 2013 Use This SQL: SELECT `products_name`,SUM(`quantity`) as Quantity FROM `product` Group By `products_id` Order By Quantity DESC Link to comment https://forums.phpfreaks.com/topic/277281-most-popular-id-in-a-row-in-a-table/#findComment-1426482 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.