如何按 DESC 或 ASC 对 varchar 数字列进行排序?

发布于 2024-12-09 20:46:20 字数 195 浏览 0 评论 0原文

我写...

ORDER BY column ASC

但我的列是 VARCHAR 并且它排序错误,如 1, 10, 2,而不是 1, 2, 10

我怎样才能像 1, 2, 10 那样排序?

I write ...

ORDER BY column ASC

but my column is VARCHAR and it sorts wrong like 1, 10, 2, instead of 1, 2, 10.

How can I do it to sort like 1, 2, 10?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝颜夕 2024-12-16 20:46:20
order by 
   cast(column as float)

注意:

  • 假设您的列中只有数字。没有“鱼”或“自行车”
  • 空字符串 CAST 为零

编辑:对于 MySQL。 您无法转换为浮动

order by 
   cast(column as decimal(38,10))
order by 
   cast(column as float)

Notes:

  • Assumed you only have numbers in the columns. No "fish" or "bicycle"
  • empty strings CAST to zero

Edit: For MySQL. You can not cast to float

order by 
   cast(column as decimal(38,10))
迷你仙 2024-12-16 20:46:20

您可以转换为 int...

order by cast(column as int)

DEMO

DECLARE @q as table(
name varchar(50),
columnn varchar(10)

)
insert into @q
VALUES('one','1'),('one','10'),('one','20'),('one','3'),('one','2'),('one','20')


select * from @q order by cast  (columnn as int) desc

打印

-------------------------------------------------- ----------
one                                                20
one                                                20
one                                                10
one                                                3
one                                                2
one                                                1

所以,丹尼尔,是的,它有效:)

更新

order by cast(column as decimal(20,6))

将列值转换为最多 20 位数字和 6 位的十进制数字小数位。根据您的实际要求进行调整。

You can cast to int...

order by cast(column as int)

DEMO

DECLARE @q as table(
name varchar(50),
columnn varchar(10)

)
insert into @q
VALUES('one','1'),('one','10'),('one','20'),('one','3'),('one','2'),('one','20')


select * from @q order by cast  (columnn as int) desc

prints

-------------------------------------------------- ----------
one                                                20
one                                                20
one                                                10
one                                                3
one                                                2
one                                                1

So, Daniel, yes, it works :)

UPDATE:

order by cast(column as decimal(20,6))

Will cast the column values to decimal numbers with 20 digits max and 6 decimal places. Adjust it to your actual requirements.

一曲琵琶半遮面シ 2024-12-16 20:46:20

试试这个:

order by CAST(column as UNSIGNED)

Try this:

order by CAST(column as UNSIGNED)
谈场末日恋爱 2024-12-16 20:46:20

我用这种方式
将其乘以 1,查询为:

ORDER BY 列名 * 1 ASC

示例:表 user 具有列值 [varchar(20)] 的值。
然后就可以查询了:

SELECT * FROM 用户 ORDER BY 值 * 1

在我们乘以它之后,MySQL 会将其视为一个数字,但不建议在重负载时使用这种方式。

i used this way
multiply it with one the query is :

ORDER BY columnname * 1 ASC

Example: Table user have value with column value [varchar(20)].
Then you can query it:

SELECT * FROM user ORDER BY value * 1

After we multiply it MySQL will treat it like a number but this way is not recommended for a heavy load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文