我有问题。我想安排列的减少顺序。 iphone4,iphone5,iphone6。例如:
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
这是我的mysql
Select Name, SUM(iphone4) ,SUM(iphone5),SUM(iphone6) from orders group by Name order by iphone4 DESC, iphone5 DESC, iphone6 DESC
但它只是工作栏iphone4和iphone5。其余的,iphone6它没有工作 谁能帮帮我?
答案 0 :(得分:0)
如果值存储为字符串而不是数字,则订单似乎无效。
如果是这种情况,那么将它们转换为数字可以解决问题:
Select m.*
from mark m
order by (mathematics + 0) DESC, (physics + 0) DESC, (chemistry + 0) DESC;
此外,此查询似乎不需要GROUP BY
。
编辑:
对于已修改的版本,您需要order by
以及select
中的聚合。为了明确,我会将它们写成:
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;
答案 1 :(得分:0)
如Cast Functions and Operators:
中所述结果的类型可以是以下值之一:
- BINARY [(N)]
- CHAR [(N)]
- DATE
- DATETIME
- DECIMAL [(M [,d)]
- 签名[INTEGER]
- TIME
- UNSIGNED [INTEGER]
所以你的查询就像那样
Select m.*
from mark m
order by CAST(mathematics AS UNSIGNED) DESC, CAST(physics AS UNSIGNED) DESC, CAST(chemistry AS UNSIGNED) DESC;