读书人

问个排序的有关问题

发布时间: 2012-03-05 11:54:02 作者: rapoo

问个排序的问题


中文¥1
中文¥2
中文

如何让SELECT出来的语句如上排序

[解决办法]
select * from tb where 字段 = '中文¥1 '
union all
select * from tb where 字段 = '中文¥2 '
union all
select * from tb where 字段 = '中文 '

[解决办法]
--try


select * from tbName
order by
case colName when '中文¥1 ' then 1 when '中文¥2 ' then 2 when '中文 ' then 3 end
[解决办法]
declare @str varchar(1000)
set @str= '中文¥1,中文¥2,中文 '
select * from 表 order by charindex( ', '+字段+ ', ', ', '+@str+ ', ')
[解决办法]
declare @t table(c1 nvarchar(100))
insert @t
select '中文¥1 ' union all
select '中文¥3 ' union all
select '中文¥2 ' union all
select '中文¥4 ' union all
select '中文 '

select * from @t
order by case when charindex( '¥ ',c1)> 0 then 0 else 1 end,c1

/*
c1
----------
中文¥1
中文¥2
中文¥3
中文¥4
中文
*/

读书人网 >SQL Server

热点推荐