当sql server数据表中某个字段值为 第一组,第二组,第三组…时,如果想对其进行排序,按照常规查询得到的结果如下:
select a.group_name, count(*) as cnt from tablea a group by a.group_name order by a.group_name asc
并不能得到“一二三四五”的顺序。
这时需要sql server里的两个函数,substring() 和 charindex(),sql语句如下:
select a.group_name, count(*) as cnt, charindex(substring(a.group_name,2,1),'一二三四五六七八九十') as place from tablea a group by a.group_name order by place
增加一个临时place 字段。
如果你需要hibernate和sqlserver集成,则需要把charindex函数换成locate函数
select a.groupname, count(*) as cnt, locate(substring(a.groupname,2,1),'一二三四五六七八九十') as place from table a group by a.groupname order by place