这样可以用SQL语句完成吗?

assiwe 2011-05-20 03:55:48
因为某些原因,我不想用存储过程来写.

一个表里有如下字段
id pid mark
11
21
31
4 1
5 1
6 1

id 不重,not null ,但不一定连续.
pid现在是空的.
mark里是1 或者是null
我现在希望把mark是1的pid用 mark是null的的id填上.但是不能重复
比如
id pid mark
11
21
31
4 11 1
5 31 1
6 21 1


请问有没有办法用sql语句做到?
...全文
81 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
assiwe 2011-05-20
  • 打赏
  • 举报
回复
算了 ,我写程序改吧. 谢谢各位了.
assiwe 2011-05-20
  • 打赏
  • 举报
回复
数据库是sql2000...
喜-喜 2011-05-20
  • 打赏
  • 举报
回复
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
id int,
pid int,
mark int
)
go
--插入测试数据
insert into tb select 11,null,null
union all select 21,null,null
union all select 31,null,null
union all select 4,null,1
union all select 5,null,1
union all select 6,null,1
go
--代码实现

update t1 set pid=aid
from tb t1,( select aid=a.id,bid=b.id from (
select *,idd=row_number()over(order by getdate()) from tb where mark is null ) a,(
select *,idd=row_number()over(order by getdate()) from tb where mark is not null )b
where a.idd=b.idd )t2
where t1.id=t2.bid

select * from tb

/*测试结果

id pid mark
---------------------
11 NULL NULL
21 NULL NULL
31 NULL NULL
4 11 1
5 21 1
6 31 1

(6 行受影响)
*/
AcHerat 元老 2011-05-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 assiwe 的回复:]
引用 7 楼 acherat 的回复:

SQL code

create table tb(id int,pid int,mark int)
insert into tb
select 11,null,null union all
select 21,null,null union all
select 31,null,null union all
select 4,null,……
[/Quote]

如果是什么规律都没,那就很麻烦了,难道要用游标去一条一条插入并判断?
wqzone 2011-05-20
  • 打赏
  • 举报
回复
声明:
1、可能t1代表,你的那个婊。
2、可能 mark为空的很少,这样就更新不完
3、row_num()为获取行号 写的有点问题自己去查查,他是一个innerjoin 的条件
4、思路

把数据分成两部分
mark为1的部分 t_a
mark为null的部分 t_b

获取 t_b的id(key) 和 t_a的pid(value) ,是以行号为关联条件。

再与t_1对比(以key和id为关联条件)进行关联。
5、请不要嫌我啰嗦,思路是没问题的。我没有调试,自己去试试。

祝你愉快


update
t1 set t1.pid = t2.value
from t1
inner join
(

select
t_a.id as key , t_b.id as value
from
(
select id , ROW_NUMBER() as rownum from t1
where mark = 1
) as t_a inner join
(
select id , ROW_NUMBER() as rownum from t1
where mark is null
) as t_b on ( t_a.rownum = t_b.rownum )

) as t2 on ( t1.id = t2.key )
assiwe 2011-05-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 acherat 的回复:]

SQL code

create table tb(id int,pid int,mark int)
insert into tb
select 11,null,null union all
select 21,null,null union all
select 31,null,null union all
select 4,null,1 union all
select 5,null,1 ……
[/Quote]
这个id是没有规律的,只是惟一
AcHerat 元老 2011-05-20
  • 打赏
  • 举报
回复

create table tb(id int,pid int,mark int)
insert into tb
select 11,null,null union all
select 21,null,null union all
select 31,null,null union all
select 4,null,1 union all
select 5,null,1 union all
select 6,null,1
go

update b
set b.pid = a.id
from tb a,tb b
where a.mark is null and b.mark is not null
and cast(left(ltrim(a.id),1) as int) + 3 = b.id

select * from tb

drop table tb

/*

id pid mark
----------- ----------- -----------
11 NULL NULL
21 NULL NULL
31 NULL NULL
4 11 1
5 21 1
6 31 1

(6 行受影响)

*/

--楼主应该再清楚一些。
assiwe 2011-05-20
  • 打赏
  • 举报
回复
当一致好了, 或者不一致的话剩下的不管也行.
dawuguj 2011-05-20
  • 打赏
  • 举报
回复
你搞个电子表格复制好了
dawuguj 2011-05-20
  • 打赏
  • 举报
回复
你的结果不唯一的规则让语句实现?

快溜 2011-05-20
  • 打赏
  • 举报
回复
为null和为1不一定对半,那样就存在有些为一的pid依然为null
百年树人 2011-05-20
  • 打赏
  • 举报
回复
两者数量不一致时如何处理?
assiwe 2011-05-20
  • 打赏
  • 举报
回复
CSDN要怎么排版啊?

34,590

社区成员

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

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