douke1891 2016-08-26 17:07
浏览 48

php mysql按多列选择顺序

I have a problem . I want arrange order decrease of column. iphone4 ,iphone5, iphone6. For example:

Name |iphone4 |iphone5|iphone6 
john | 6      | 7     | 8      
Smith| 6      | 9     | 8      
Louis| 6      | 7     | 10      
Nick | 7      | 7     | 8      
john | 2      | 7     | 4 
Smith| 7      | 7     | 5

this is my mysql

Select Name, SUM(iphone4) ,SUM(iphone5),SUM(iphone6) from orders group by Name order by iphone4  DESC, iphone5 DESC, iphone6  DESC

but it's only working column iphone4 and iphone5. the rest, iphone6 it's don't working Who can help me?

  • 写回答

2条回答 默认 最新

  • doutun1362 2016-08-26 17:11
    关注

    The order would seem to not be working if the values were stored as strings instead of numbers.

    If this is the case, then converting them to numbers fixes the problem:

    Select m.*
    from mark m
    order by (mathematics + 0) DESC, (physics + 0) DESC, (chemistry + 0) DESC;
    

    In addition, no GROUP BY seems to be needed for this query.

    EDIT:

    For the edited version, you need the aggregations in the order by as well as the select. To be explicit, I would write these as:

    Select Name, SUM(iphone4+0), SUM(iphone5+0), SUM(iphone6+0)
    from orders
    group by Name
    order by SUM(iphone4+0) desc, SUM(iphone5+0) desc, SUM(iphone6+0) desc;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错