高难 UPDATE 语句,怎么写??

pwlxj 2005-11-19 05:35:21
我要建一个学校沙龙
当张三注册时有介绍人:李四,并且给介绍人李四 积1分 而且李四的介绍人:王五也要积1分,而王五的介绍人:赵六也要积1分,赵六的介绍人:杨七也要积1分,杨七的介绍人:刘八也要积1分只这么五级(只能五级)
当然这个李四,王五、赵六、杨七、刘八都是已经注册了的用户
这个 UPDATE 语句怎么写呢??

先感谢!!!!!!!!
...全文
485 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyuan0773 2005-12-02
  • 打赏
  • 举报
回复
drop trigger trig1
go
create trigger trig1 on T1
for insert
as

declare @m int,@SubPid int
set @m=1
select @Pid=Pid,@subPid=SubPid from inserted

update t1 set Quantity=Quantity+1 where pid=@SubPid

while @@rowcount>0
begin
set @m=@m+1
select @SubPid=SubPid from T1 where pid=@SubPid
if @m<6
update t1 set Quantity=Quantity+1 where pid=@SubPid

end
feiyuan0773 2005-12-02
  • 打赏
  • 举报
回复
create t1(pid int,subpid int,quantity int)
subpid 为推荐人
create trigger trig1 on T1
for insert
as
declare @t Table(Pid int,SubPid int,level2 int)
declare @m int,@n int,@SubPid int,@Pid int
set @m=1
select @Pid=Pid,@subPid=SubPid from inserted
if @SubPid>0
insert @t select Pid,subPid,@m from T1 where Pid=@SubPid

while @@rowcount>0
begin
set @m=@m+1
if @m<6
insert @t select Pid,SubPid,@m from T1 where Pid in(select subPid from @t where level2=@m-1)
end

update T1 set Quantity=Quantity+1 where Pid in (select Pid from @t)
samfeng_2003 2005-12-01
  • 打赏
  • 举报
回复
好了,改成触发器了!:)
只不过感觉有些地方可以优化一下!

create table t
(id int,j_id int,姓名 varchar(20),积分 int)

/*---只是初试数据,所以没有对积分进行考虑,只是考虑每插入一条数据,那么积分相应变化
--*/
insert t
select 1,0,'刘八',0

go
/*------------建立触发器-----------------*/
create trigger t_go
on t
for insert
as
set xact_abort on
begin tran
declare @t table (id int,j_id int,姓名 varchar(20),积分 int,level int)
declare @t1 table(id int,j_id int,姓名 varchar(20),积分 int)
declare @i int,@k int
insert @t1
select * from inserted
select @k=count(*) from @t1
while @k>0
begin
select @i=1
insert @t
select top 1 *,@i from @t1 order by id asc
delete from @t1 where id = (select top 1 id from @t1 order by id asc)
while exists (select * from t a,@t b where a.id=b.j_id and b.level=@i and @i<=5)
begin
set @i=@i+1
insert @t
select a.id,a.j_id,a.姓名,a.积分+1,@i from t a,@t b where a.id=b.j_id and b.level=@i-1
end
update t set 积分=b.积分
from t a,@t b where a.id=b.id
delete from @t
set @k=@k-1
end
commit tran
go

insert t
select 2,1,'李四',0 union all
select 3,2,'冯七',0 union all
select 4,3,'赵九',0 union all
select 5,4,'刘十',0 union all
select 6,5,'王五',0 union all
select 7,6,'周六',0 union all
select 8,3,'胡三',0 union all
select 9,8,'苟一',0

/*-----------显示结果---------------*/
select * from t

insert t
select 11,10,'123',0
select * from t

/*-----------删除环境---------------*/
drop trigger t_go
drop table t

id j_id 姓名 积分
----------- ----------- -------------------- -----------
1 0 刘八 7
2 1 李四 7
3 2 冯七 6
4 3 赵九 3
5 4 刘十 2
6 5 王五 1
7 6 周六 0
8 3 胡三 1
9 8 苟一 0

(所影响的行数为 9 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)

id j_id 姓名 积分
----------- ----------- -------------------- -----------
1 0 刘八 7
2 1 李四 7
3 2 冯七 6
4 3 赵九 3
5 4 刘十 2
6 5 王五 1
7 6 周六 0
8 3 胡三 1
9 8 苟一 0
11 10 123 0

(所影响的行数为 10 行)
diaowf 2005-12-01
  • 打赏
  • 举报
回复
Top
junhao_666 2005-12-01
  • 打赏
  • 举报
回复
学习
gaojie001 2005-12-01
  • 打赏
  • 举报
回复
aa
软侠 2005-12-01
  • 打赏
  • 举报
回复
路過幫頂,同時
盼望有高手幫我解決我的問題,地址:
http://community.csdn.net/Expert/topic/4430/4430395.xml?temp=.3242304
samfeng_2003 2005-11-30
  • 打赏
  • 举报
回复
呵呵!要看名侦探科南,所以就写到这里了!

/*--------建立环境---------*/
create table t
(id int,j_id int,姓名 varchar(20),积分 int)

/*---只是初试数据,所以没有对积分进行考虑,只是考虑每插入一条数据,那么积分相应变化
--*/
insert t
select 1,0,'刘八',0 union all
select 2,1,'李四',0 union all
select 3,2,'冯七',0 union all
select 4,3,'赵九',0 union all
select 5,4,'刘十',0 union all
select 6,5,'王五',0

/*------------该表的作用等同于触发器中的inserted,此时表示触发器中传入三条数据------------------*/
create table t1
(id int,j_id int,姓名 varchar(20),积分 int)

insert t1
select 7,6,'周六',0 union all
select 8,3,'胡三',0 union all
select 9,4,'苟一',0


/*---------------开始程序,你可以该成触发器就OK------------------——*/
declare @t table (id int,j_id int,姓名 varchar(20),积分 int,level int)
declare @i int,@k int
select @k=count(*) from t1
while @k>0
begin
select @i=1
insert @t
select top 1 *,@i from t1 order by id asc
insert t
select top 1 * from t1 order by id asc
delete from t1 where id = (select top 1 id from t1 order by id asc)
while exists (select * from t a,@t b where a.id=b.j_id and b.level=@i and @i<=5)
begin
set @i=@i+1
insert @t
select a.id,a.j_id,a.姓名,a.积分+1,@i from t a,@t b where a.id=b.j_id and b.level=@i-1
end
update t set 积分=b.积分
from t a,@t b where a.id=b.id
delete from @t
set @k=@k-1
end

/*-----------显示结果---------------*/
select * from t


/*-----------删除环境---------------*/
drop table t
drop table t1


id j_id 姓名 积分
----------- ----------- -------------------- -----------
1 0 刘八 2
2 1 李四 3
3 2 冯七 3
4 3 赵九 2
5 4 刘十 1
6 5 王五 1
7 6 周六 0
8 3 胡三 0
9 4 苟一 0

(所影响的行数为 9 行)
zhao606 2005-11-30
  • 打赏
  • 举报
回复
在触发器里update 并且更新是在一个函数里的,该函数会递归调用自己,当达到五次后递归调用结束
pwlxj 2005-11-20
  • 打赏
  • 举报
回复
我没试好,还请赐教!:我的表和表间关系
用户表
字段:姓名(主键),推荐人,
推荐人表
字段:姓名(外键),积分
pwlxj 2005-11-19
  • 打赏
  • 举报
回复
感谢!
我先试一试!
winehero 2005-11-19
  • 打赏
  • 举报
回复
树叶结点到根节点遍历问题,用递归或临时堆栈表可解决
zlp321002 2005-11-19
  • 打赏
  • 举报
回复
--贴出表关系.
最好贴个测试数据...
--不知道你表是怎么建的!思路:就是求一个树的所有子接点.......

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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