SQL排序问题(高手请指教)

来源:百度知道 编辑:UC知道 时间:2024/06/12 07:42:31
select 年龄,姓名 from a1 where 姓名 in ("张三","李四","王五","阿牛")
要求根据where条件括号 里面姓名的顺序来输出结果.

不同的状况不同的处理,如果数据量很少,那么手工编写编号

select * from a1 a,
(select 1 ids,'张三' flag
union all
select 2,'李四'
union all
select 3, '王五'
union all
select 4,'阿牛') b where a.names=b.flag
order by b.ids

大量数据情况下,把b表插入到一个有标识得临时表中,比如

create table #a
(
ids int identity(1,1)
,names varchar(32)
)然后再和a1关联查询
===========================
有人说union all都是错的,别人写的我管不了,但你说我的错,我可不同意。
无论是谁,只要证明出我的代码有误,奖励500分!决不食言!!!

select 年龄,姓名 from a1 where 姓名 in ("张三")
union all
select 年龄,姓名 from a1 where 姓名 in ("李四")
union all
select 年龄,姓名 from a1 where 姓名 in ("王五")
union all
select 年龄,姓名 from a1 where 姓名 in ("阿牛")

SELECT 年龄,姓名 FROM (
select 年龄,姓名,1 seq from a1 WHERE 姓名 = '张三'
UNION
select 年龄,姓名,2 seq from a1 WHERE 姓名 = '李四'
UNION
select 年龄,姓名,3 seq from