1

I want to get those Product in ascending order who number is greater in mysql

  order_id    |     product_id    
     1        |         13    
     1        |         12    
     1        |         24    
     2        |         14    
     2        |         245    
     2        |         23    
     3        |         14    
     3        |         23    
     4        |         14    

i have done that code but its not working

SELECT  product_id  FROM `sales_order_item`  ORDER BY COUNT('product_id') ASC

i want priduct_id in ascending order on the basis of there count\

product_id
    14
    23
    13
    24
    245

as 14 occurs 4 times it must have to be on top and so on

Fabian S.
  • 2,441
  • 2
  • 24
  • 34
Waqar Ali
  • 163
  • 13

1 Answers1

1

use order by COUNT('product_id') desc

SELECT  product_id,COUNT('product_id') as cnt  FROM `sales_order_item` 
group by product_id 
ORDER BY cnt desc
Fahmi
  • 37,315
  • 5
  • 22
  • 31
  • `Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query` – Waqar Ali Jun 19 '19 at 05:55