Jump to content

most popular id in a row in a table


1internet

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.