【问题标题】:How i can get values order by the count of distinct values我如何通过不同值的计数来获取值顺序
【发布时间】:2019-11-01 18:55:29
【问题描述】:

我想按升序获取mysql中编号较大的那些产品

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

我已经完成了该代码,但它不起作用

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

我希望 priduct_id 在计数的基础上按升序排列\

product_id
    14
    23
    13
    24
    245

因为 14 出现了 4 次,所以它必须在顶部等等

【问题讨论】:

    标签: mysql magento2


    【解决方案1】:

    使用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
    

    【讨论】:

    • Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多