读书人

求教这句SQL该怎么写 最好

发布时间: 2013-07-01 12:33:04 作者: rapoo

求教这句SQL该如何写 最好
数据结构:

有一张临时表#t,有三列,分别为A,B,C

具体数据如下

A B C
-------------
1 1 1
1 2 0
1 3 1
1 4 0

现想做到把C=0的某行数据,插入该行下面

结果

A B C
-------------
1 1 1
1 2 0
1 2 0
1 3 1
1 4 0
1 4 0

我使用的SQL语句如下:


create table #t
(
A int,
B int,
C int
)

insert into #t
select 1,1,1 union all
select 1,2,0 union all
select 1,3,1 union all
select 1,4,0

select * from #t
union all
select * from #t where C = 0
order by B,C


想求教论坛各位达人,有没有更好的算法

谢谢 SQL 算法
[解决办法]



create table #t
(
A int,
B int,
C int
)

insert into #t
select 1,1,1 union all
select 1,2,0 union all
select 1,3,1 union all
select 1,4,0
go
insert into #t
select * from #t where C =0
go
select * from #t

[解决办法]
create table #t
(
A int,
B int,
C int
)

insert into #t
select 1,1,1 union all
select 1,2,0 union all
select 1,3,1 union all
select 1,4,0
go
insert into #t
select * from #t where C =0
go
select * from #t
ORDER BY b,c

/*
A B C
----------- ----------- -----------
1 1 1


1 2 0
1 2 0
1 3 1
1 4 0
1 4 0

*/

借用一下早恋的代码,其实5楼已经写出来了

读书人网 >SQL Server

热点推荐