高分求一SQL语句写法

来源:百度知道 编辑:UC知道 时间:2024/06/06 01:13:55
从数据库一张表中查询所有数据,将其中指定的一行记录放在查询结果的第一行显示。
例如:表总有四行数据 A
B
C
D
四行数据,要求查询结果为:将C放在查询结果的第一行显示
即: C
A
B
D

select 字段名 from 表名 order by (case when 字段名='C' then 0 end),字段名

你指定的条件是什么呢??
还是随意?
如果是有id之列的主键,我楼上的sql就能做到。

select id,name from 表 where id=你要提前的id
union all
select id,name from 表 where id<>你要提前的id

(select id,name from 表 where 列XX='A'
order by 列XX)
union all
(select id,name from 表 where 列XX<>'A'
order by 列XX)

为了更灵活,建议写存储过程
create proc paixu
@id varchar(10)
as
declare @count int
begin
select @count=count(*) from 表名 where 列名=@id
if @count>0
begin
select * from 表名 where 列名=@id
union all
select * from 表名 where 列名!=@id
end
else
begin
print @id+'是无效的'
end
end

exec paixu 'C'

select * from table1 where column1="C"
union all
select * from table1 where column1<>"C"

用select语句就行了呀,吧C的ID放前面