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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?