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
解决方案 »
- 高分求助各位高手门如何提高一下我的数据库的性能
- 急:数据库错误:进程 90 发生了严重的异常 c0000005 EXCEPTION_ACCESS_VIOLATION
- 为SQL Server构造灵活可靠的安全门
- 采用Navicat导入备份时出现Stream read error
- 向数据库登陆用户隐藏非系统表
- 如何用一条语句完成下列功能:查找学生表中是否有学生"甲"和教师表中是否有教师“乙”,若二者都存在,返回真,若有一个或两个都不存在,
- SQL SERVER不同语言版本之间转换的注意事项?
- 求教一条SQL命令
- 请教一个存储过程问题
- 大家好...緊急問題
- 在线等待~~怎么取前日期的前15天 希望帮助我
- 请大家帮我写一个如下的存储过程,谢谢!
先select id ,name from t where id in (2,1,4,3)得到结果集:id name
1 q
2 r
3 3
4 5怎么样才能按2,1,4,3的顺序插到临时表中?
create table t1(id int)
里面你需要的顺序插入,比如2,1,4,3insert into t1
select 2
union all select 1
union all select 4
union all select 3declare @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
insert into #tmp select id,name from table1 where id=@id --按t1中的id顺序插数据到临时表
FETCH NEXT FROM c_test INTO @id
End
Close c_test
deallocate c_testselect * from #tmp --从临时表中获得所需要的数据
----------- ----
2 r
1 q
4 5
3 3(所影响的行数为 4 行)
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=2817
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 select * from #tmp
order by (case id when 2 then 'A' when 1 then 'B' when 4 then 'C' when 3 then 'D' end)select id ,name from t where id in (1,4,2,3)
order by (case id when 1 then 'A' when 4 then 'B' when 2 then 'C' when 3 then 'D' end)
order by case name when 'r' then 1
when 'q' then 2
when '5' then 3
when '3' then 4
end--测试结果
/*
id name
----------- ----------
2 r
1 q
4 5
3 3(所影响的行数为 4 行)
*/
宁哥辛苦了,还专门找出用charIndex和subString分隔字符串的方法,其实这个小弟自己也会写的,呵呵,再次感谢!我以前没想到一个一个选出来插到临时表里面,老是想怎么能排序,钻牛角尖了以后有问题还要请大家指点