求教这句SQL该如何写 最好

oreoconansisu 2013-05-27 06:37:39
数据结构:

有一张临时表#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


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

谢谢
...全文
118 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
oreoconansisu 2013-05-27
  • 打赏
  • 举报
回复
引用 11 楼 DBA_Huangzj 的回复:
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楼已经写出来了
發糞塗牆 2013-05-27
  • 打赏
  • 举报
回复
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楼已经写出来了
oreoconansisu 2013-05-27
  • 打赏
  • 举报
回复
引用 9 楼 DBA_Huangzj 的回复:
那个其实可以,只是要在order by上面做点手脚而已
我想出两种方法 一种是加个Id标示 另一种是把指定位置起至最后的所有行插入#temp,再删除这部分数据,最后再insert回来 版主说的order by 求进一步指教
發糞塗牆 2013-05-27
  • 打赏
  • 举报
回复
那个其实可以,只是要在order by上面做点手脚而已
oreoconansisu 2013-05-27
  • 打赏
  • 举报
回复
引用 4 楼 ZaoLianBuXiQi 的回复:
[quote=引用 2 楼 oreoconansisu 的回复:] [quote=引用 1 楼 ZaoLianBuXiQi 的回复:]



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
是一种办法 另外,光用select能不能做到这种效果[/quote] 你的要求:现想做到把C=0的某行数据,插入该行下面 查的话,就用你那种方法 [/quote] 好的,谢谢
oreoconansisu 2013-05-27
  • 打赏
  • 举报
回复
引用 6 楼 DBA_Huangzj 的回复:
额,我知道了,你是不是想在插入的基础上保持“紧跟其后”?
我本意是查询,不过你说的这个 有无可能?
發糞塗牆 2013-05-27
  • 打赏
  • 举报
回复
额,我知道了,你是不是想在插入的基础上保持“紧跟其后”?
vioalouyang 2013-05-27
  • 打赏
  • 举报
回复

INSERT INTO #t SELECT * FROM #t where C=0

SELECT * from #t ORDER BY B,C
MrYangkang 2013-05-27
  • 打赏
  • 举报
回复
引用 2 楼 oreoconansisu 的回复:
[quote=引用 1 楼 ZaoLianBuXiQi 的回复:]



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
是一种办法 另外,光用select能不能做到这种效果[/quote] 你的要求:现想做到把C=0的某行数据,插入该行下面 查的话,就用你那种方法
發糞塗牆 2013-05-27
  • 打赏
  • 举报
回复
引用 楼主 oreoconansisu 的回复:
数据结构: 有一张临时表#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
想求教论坛各位达人,有没有更好的算法 谢谢
你的方法好像的确没必要哦,早恋的那个到可以,不过不知道是不是你想要的
oreoconansisu 2013-05-27
  • 打赏
  • 举报
回复
引用 1 楼 ZaoLianBuXiQi 的回复:



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
是一种办法 另外,光用select能不能做到这种效果
MrYangkang 2013-05-27
  • 打赏
  • 举报
回复



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

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧