按某一字段排序,先按字母升序再按数字降序排
例如
字段
ab1
ab2
ab11
bc2
bc12
cd2
排序之后应为
字段
ab11
ab2
ab1
bc12
bc2
cd2
HQL语句应该怎么写,求高手解答?
例如
字段
ab1
ab2
ab11
bc2
bc12
cd2
排序之后应为
字段
ab11
ab2
ab1
bc12
bc2
cd2
HQL语句应该怎么写,求高手解答?
解决方案 »
- 如何找出每个用户表中所有有数据的列
- 请大虾们告诉我这样的的类型怎么转换?
- 请问“数据库的表中有相同的重复的内容(ID号和内容相同),如何把相同的内容删去,只留下一条记录呢?”
- 安装SQLSERVER2000企业版时提示:网络不正常,安装失败!!!我装了网卡,就是没有联网!
- sql2005同步问题 急大哥们帮忙
- 为什么我只能以IP地址访问SQL服务器?在线求教!
- 这样做的好处是什么?
- SQL 2000的数据是否可以复制到7.0当中?如果可以,请给出详细的步骤,谢谢。
- 关于Insert Into ... Select的用法
- 怎样得到有输出参数的存储过程 返回的参数
- 一张表中根据时间段判断时间段是否重复
- 请问变量可以这么赋值吗?
--确定的话如下:
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (字段 varchar(4))
insert into [tb]
select 'ab1' union all
select 'ab2' union all
select 'ab11' union all
select 'bc2' union all
select 'bc12' union all
select 'cd2'--开始查询
select * from [tb] order by LEFT(字段,2),CONVERT(int,RIGHT(字段,len(字段)-2)) desc--结束查询
drop table [tb]/*
字段
----
ab11
ab2
ab1
bc12
bc2
cd2(6 行受影响)
go
--> -->
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([字段] nvarchar(10))
Insert #T
select N'ab1' union all
select N'ab2' union all
select N'ab11' union all
select N'bc2' union all
select N'bc12' union all
select N'cd2'
Go
Select *
from #T
ORDER BY LEFT([字段],PATINDEX('%[0-9]%',[字段])-1),CAST(RIGHT([字段],PATINDEX('%[^0-9]%',REVERSE([字段]))-1) AS INT)
/*
ab1
ab2
ab11
bc2
bc12
cd2
*/
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (字段 varchar(4))
insert into [tb]
select 'ab1' union all
select 'ab2' union all
select 'ab11' union all
select 'bc2' union all
select 'bc12' union all
select 'cd2'--开始查询
select * from tb
order by left(字段,patindex('%[0-9]%',字段)-1),
convert(int,substring(字段,patindex('%[0-9]%',字段),len(字段))) desc--结束查询
drop table [tb]/*
字段
----
ab11
ab2
ab1
bc12
bc2
cd2(6 行受影响)
再从第一个数字的位置一直取到最后一位,就是数字的位置,转为数字型,倒序
希望LZ看得懂
具体的你根据hql语法自己改吧,hql我是不会。