按房号的SQL订单地址

时间:2014-04-28 10:56:50

标签: mysql sql

所以,如果我有结果

11 The street
9 The street 
59 The street

常规ORDER BY ASC执行此操作

11 The street
59 The street 
9 The street

如何按ASC订购,并将数字计算为

9 The street
11 The street 
59 The street

2 个答案:

答案 0 :(得分:2)

请尝试:

select *
From tbl
order by cast(Left(Col, PatIndex('%[^0-9]%', Col)) as int)

SQL Fiddle Demo

对于MySql,请尝试:

select *
From tbl
order by convert(SUBSTRING_INDEX(Col, ' ', 1), UNSIGNED INTEGER)

MySql Fiddle

答案 1 :(得分:0)

另一种解决方案(有点简单);只是按照您的示例数据进行操作,并假设您的所有数据字符串都有The Street个共同点。另外,TechDo所建议的应该是我将要去的那个(对于通用解决方案)

select * from tbl
order by 
cast(
(substring(col,0,charindex('The street',col))) as int)