在SQL中将最大值排序为最小值无法正常工作 [英] sorting max to min value in sql is not working properly
问题描述
我有1张桌子用于ex table1 它具有列名称:id,名称,金额.
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.
推荐答案
您正在将amount
存储为字符串,它会出现.
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
通常,在SQL中,应使用内置类型并将值存储为适当的时间.数字应存储为数字(整数,浮点数或小数).日期和时间应存储为日期/时间类型.字符串可以用作字符串值.
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.
这篇关于在SQL中将最大值排序为最小值无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!