dongzhiju0324 2015-06-29 10:38
浏览 105
已采纳

在sql中排序max到min值无法正常工作

i have 1 table for ex table1 and it have columns names: id, name, amount.

id name amount
1  abc  20
2  xyz  50
3  pqr  246

i want to sort it from max value to min. i have written this query:

SELECT * FROM table1 ORDER BY amount DESC;

but it gives output like this:

id name amount
2  xyz  50
3  pqr  246
1  abc  20

which is not valid output that i want, i want output like this:

id name amount
3  pqr  246
2  xyz  50
1  abc  20

so, if anyone know the solution of this problem please give me the solution.

  • 写回答

4条回答 默认 最新

  • douhuanbai6729 2015-06-29 10:42
    关注

    You are storing the amount as a string, it would appear.

    Here are two solutions. You can convert the values to a number for the sort:

    order by amount + 0 desc
    

    Or, order by the length first and then the value:

    order by length(amount) desc, amount desc
    

    In general, in SQL, you should make use of the built-in types and store values as the appropriate time. Numbers should be stored as numbers (integers, floats, or decimals). Dates and times should be stored as date/time types. And strings can be used for string values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算