如何按指定的顺序获取数据
2005-01-01 12:43
489 查看
原贴地址:http://community.csdn.net/Expert/topic/3693/3693091.xml?temp=.6086542 http://community.csdn.net/Expert/TopicView.asp?id=3724743
测试table
create table table1 (id int,name char)
insert into table1
select 1,'q'
union all select 2,'r'
union all select 3,'3'
union all select 4,'5'
要求按指定的id顺序(比如2,1,4,3)排列获取table1的数据
方法1:使用union all,但是有256条数据的限制
select id,name from table1 where id=2
union all
select id,name from table1 where id=1
union all
select id,name from table1 where id=4
union all
select id,name from table1 where id=3
方法2:在order by中使用case when
select id ,name from t where id in (2,1,4,3)
order by (case id
when 2 then 'A'
when 1 then 'B'
when 4 then 'C'
when 3 then 'D' end)
*以上两种方法适合在数据量非常小的情况下使用
方法3:使用游标和临时表
先建一个辅助表,里面你需要的顺序插入,比如2,1,4,3
create table t1(id int)
insert into t1
select 2
union all select 1
union all select 4
union all select 3
declare @id int --定义游标
declare c_test cursor for
select id from t1
select * into #tmp from table1 where 1=2 --构造临时表的结构
OPEN c_test
FETCH NEXT FROM c_test
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
--按t1中的id顺序插数据到临时表
insert into #tmp select id,name from table1 where id=@id
FETCH NEXT FROM c_test INTO @id
End
Close c_test
deallocate c_test
*该方法适合需要按照辅助表的顺序重排table的顺序时使用
(即辅助表已经存在的情况)
方法4:分割字符串参数
select * into #tmp from table1 where 1=2 --构造临时表的结构
declare @str varchar(300),@id varchar(300),@m int,@n int
set @str='2,1,4,3,' ---注意后面有个逗号
set @m=CHARINDEX(',',@str)
set @n=1
WHILE @m>0
BEGIN
set @id=substring(@str,@n,@m-@n)
--print @id
insert into #tmp select id,name from table1 where id=convert(int,@id)
set @n=@m+1
set @m=CHARINDEX(',',@str,@n)
END
方法5:使用charindex
select * from t1 order by charindex(convert(varchar(2),id),'2,1,4,3')
测试结果
id name
----------- ----
2 r
1 q
4 5
3 3
(所影响的行数为 4 行)
测试table
create table table1 (id int,name char)
insert into table1
select 1,'q'
union all select 2,'r'
union all select 3,'3'
union all select 4,'5'
要求按指定的id顺序(比如2,1,4,3)排列获取table1的数据
方法1:使用union all,但是有256条数据的限制
select id,name from table1 where id=2
union all
select id,name from table1 where id=1
union all
select id,name from table1 where id=4
union all
select id,name from table1 where id=3
方法2:在order by中使用case when
select id ,name from t where id in (2,1,4,3)
order by (case id
when 2 then 'A'
when 1 then 'B'
when 4 then 'C'
when 3 then 'D' end)
*以上两种方法适合在数据量非常小的情况下使用
方法3:使用游标和临时表
先建一个辅助表,里面你需要的顺序插入,比如2,1,4,3
create table t1(id int)
insert into t1
select 2
union all select 1
union all select 4
union all select 3
declare @id int --定义游标
declare c_test cursor for
select id from t1
select * into #tmp from table1 where 1=2 --构造临时表的结构
OPEN c_test
FETCH NEXT FROM c_test
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
--按t1中的id顺序插数据到临时表
insert into #tmp select id,name from table1 where id=@id
FETCH NEXT FROM c_test INTO @id
End
Close c_test
deallocate c_test
*该方法适合需要按照辅助表的顺序重排table的顺序时使用
(即辅助表已经存在的情况)
方法4:分割字符串参数
select * into #tmp from table1 where 1=2 --构造临时表的结构
declare @str varchar(300),@id varchar(300),@m int,@n int
set @str='2,1,4,3,' ---注意后面有个逗号
set @m=CHARINDEX(',',@str)
set @n=1
WHILE @m>0
BEGIN
set @id=substring(@str,@n,@m-@n)
--print @id
insert into #tmp select id,name from table1 where id=convert(int,@id)
set @n=@m+1
set @m=CHARINDEX(',',@str,@n)
END
方法5:使用charindex
select * from t1 order by charindex(convert(varchar(2),id),'2,1,4,3')
测试结果
id name
----------- ----
2 r
1 q
4 5
3 3
(所影响的行数为 4 行)
相关文章推荐
- 如何按指定的顺序获取数据
- 按指定排列顺序获取数据的sql语句
- Sql Server 如何获取指定一行数据?在没有自动增长列的情况下
- *关于如何获取指定表的指定字段的数据类型的方法
- 如何获取指定表的列和数据类型
- 如何从Subversion的dump备份中获取指定目录的数据操作步骤
- C# 如何获取SQL Server 中指定数据表的所有字段名和字段类型
- 探讨PHP变量解析顺序如何获取提交数据
- 按指定排列顺序获取数据的sql语句
- 如何获取指定表的列和数据类型
- 获取DataTable中指定位置开始的数据行
- NSPredicate --(谓词查询) Foundation提供的类,它指定数据被获取或者过滤的方式
- cocos2dx如何通过Image获取指定点颜色值
- Jsoup进阶之获取指定数据
- 【百度地图API】如何调整结果面板的样式?如何获取指定页码的结果?
- JS如何实现获取表格中的单元格里面的数据,并让其累加
- 如何用线程Asynctask获取网络数据
- C编程获取指定网卡网络数据包并分析(附C语言源码)
- 如何从缓存中快速获取数据
- SQL SERVER如何获取指定数据库中所有存储过程的参数