例表:
id name
-------------
1 现金
2 信用卡
3 信用卡
4 网银
5 现金
6 手机支付
7 网银要求根据现金、信用卡、网银、手机支付的顺序排序得到结果
id name
-------------
1 现金
5 现金
2 信用卡
3 信用卡
4 网银
7 网银
6 手机支付
id name
-------------
1 现金
2 信用卡
3 信用卡
4 网银
5 现金
6 手机支付
7 网银要求根据现金、信用卡、网银、手机支付的顺序排序得到结果
id name
-------------
1 现金
5 现金
2 信用卡
3 信用卡
4 网银
7 网银
6 手机支付
石头哥太快了
order by charindex(name,'现金信用卡网银手机支付'),id
-- Author: flystone
-- Version:V1.001
-- Date:2009-08-24 11:23:53
-------------------------------------- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(id int,name nvarchar(4))
Go
Insert into ta
select 1,'现金' union all
select 2,'信用卡' union all
select 3,'信用卡' union all
select 4,'网银' union all
select 5,'现金' union all
select 6,'手机支付' union all
select 7,'网银'
Go
--Start
Select * from ta
order by charindex(name,'现金信用卡网银手机支付'),id
--Result:
/*
id name
----------- ----
1 现金
5 现金
2 信用卡
3 信用卡
4 网银
7 网银
6 手机支付(所影响的行数为 7 行)*/
--End
*
from
tb
order by
charindex(name,'现金信用卡网银手机支付'),id
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([id] int,[name] varchar(8))
insert [TB]
select 1,'现金' union all
select 2,'信用卡' union all
select 3,'信用卡' union all
select 4,'网银' union all
select 5,'现金' union all
select 6,'手机支付' union all
select 7,'网银'select * from TB order by charindex(name,'现金,信用卡,网银,手机支付')
/*
id name
----------- --------
1 现金
5 现金
2 信用卡
3 信用卡
4 网银
7 网银
6 手机支付(7 行受影响)*/
drop table TB
select * from tb
order by
case name when '现金' then 1
when '信用卡 then 2
when '网银' then 3
when '手机支付' then 4
end,id
3 else 4 end...
WHEN NAME='现金' THEN 0
WHEN NAME='信用卡' THEN 1
WHEN NAME='网银' THEN 2
WHEN NAME='手机支付' THEN END ,ID
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-08-24 11:28:21
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[name] varchar(8))
insert [tb]
select 1,'现金' union all
select 2,'信用卡' union all
select 3,'信用卡' union all
select 4,'网银' union all
select 5,'现金' union all
select 6,'手机支付' union all
select 7,'网银'
--------------开始查询--------------------------
---方法一
select
*
from
tb
order by
charindex(name,'现金信用卡网银手机支付'),id
--方法二
select * from tb
order by
case name when '现金' then 1
when '信用卡' then 2
when '网银' then 3
when '手机支付' then 4
end,id
----------------结果----------------------------
/*id name
----------- --------
1 现金
5 现金
2 信用卡
3 信用卡
4 网银
7 网银
6 手机支付(7 行受影响)
*/
WHEN NAME='现金' THEN 0
WHEN NAME='信用卡' THEN 1
WHEN NAME='网银' THEN 2
WHEN NAME='手机支付' THEN 3 END
Create table ta(id int,name nvarchar(4))
Go
Insert into ta
select 1,'现金' union all
select 2,'信用卡' union all
select 3,'信用卡' union all
select 4,'网银' union all
select 5,'现金' union all
select 6,'手机支付' union all
select 7,'网银'id name
----------- ----
1 现金
5 现金
2 信用卡
3 信用卡
4 网银
7 网银
6 手机支付(所影响的行数为 7 行)
借小F数据测试一下