求一多对多关系下的更新语句!

yhui1989love 2012-01-11 04:32:38
表 a:
UUID APP_LINE_ID
87219 513131
表 ca:
ASSET_ID APP_LINE_TF_ID APP_LINE_TK_ID
87219 494386 0
87219 513131 0
表 ar:
RELATION_ID ASSET_ID EVENT_TYPE
494386 87219 1507
494386 87219 1507
513198 87219 1508
513198 87219 1508
513131 87219 1507
513131 87219 1507
表间关系说明:
1.表a的uuid是表a是主键,同时作为表ar与ca的外键
2.表ca的APP_LINE_TF_ID,APP_LINE_TK_ID,表 ar的 RELATION_ID 实质都是表a的APP_LINE_ID,只是表a只记录最后一次发生的 APP_LINE_ID
3.ar中 EVENT_TYPE = 1507 的要对应表ca中 APP_LINE_TF_ID
4.ar中 EVENT_TYPE = 1508 的要对应表ca中 APP_LINE_TK_ID

以上数据有异常,需要的结果是让表ar变成如下结果:
ASSET_ID APP_LINE_TF_ID APP_LINE_TK_ID
87219 494386 513198
87219 513131 0

...全文
179 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yhui1989love 2012-01-16
  • 打赏
  • 举报
回复
求解答............
jwwyqs 2012-01-12
  • 打赏
  • 举报
回复
没明白 吃饭去
郗晓勇 2012-01-12
  • 打赏
  • 举报
回复
看了一上午,还是不太明白楼主什么意思,猜测楼主的需求代码写出来了,看看吧~
declare @表a table (UUID int,APP_LINE_ID int)
insert into @表a
select 87219,513131

declare @表ca table (ASSET_ID int,APP_LINE_TF_ID int,APP_LINE_TK_ID int)
insert into @表ca
select 87219,494386,0 union all
select 87219,513131,0

declare @表ar table (RELATION_ID int,ASSET_ID int,EVENT_TYPE int)
insert into @表ar
select 494386,87219,1507 union all
select 494386,87219,1507 union all
select 513198,87219,1508 union all
select 513198,87219,1508 union all
select 513131,87219,1507 union all
select 513131,87219,1507

select * from @表a
select * from @表ca
select * from @表ar

update @表ca set APP_LINE_TF_ID=(select top 1 relation_id from @表ar where EVENT_TYPE='1507') where APP_LINE_TF_ID!=(select APP_LINE_ID from @表a)
update @表ca set APP_LINE_TK_ID=(select top 1 relation_id from @表ar where EVENT_TYPE='1508') where APP_LINE_TF_ID!=(select APP_LINE_ID from @表a)
select * from @表ca

=========================================================================================

yhui1989love 2012-01-11
  • 打赏
  • 举报
回复
各位大牛 快现身吧!!!!!!!!!!!!
yhui1989love 2012-01-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 maco_wang 的回复:]

引用楼主 yhui1989love 的回复:
以上数据有异常,需要的结果是让表ar变成如下结果:
ASSET_ID APP_LINE_TF_ID APP_LINE_TK_ID
87219 494386 513198
87219 513131 0

到底是ar表还是ca表?
[/Quote]
错了 应该是ca表! 主键被我省略了 表A主键给出来了 其他两个表的主键没用 自增的UUID
结果就是吧ca表中EVENT_TYPE = 1508 的 RELATION_ID 513198 更新到表ar的APP_LINE_TK_ID中
勿勿 2012-01-11
  • 打赏
  • 举报
回复
不知所云。LZ解释清楚点。
叶子 2012-01-11
  • 打赏
  • 举报
回复
表不少,怎么连个主键都没有?

declare @表a table (UUID int,APP_LINE_ID int)
insert into @表a
select 87219,513131

declare @表ca table (ASSET_ID int,APP_LINE_TF_ID int,APP_LINE_TK_ID int)
insert into @表ca
select 87219,494386,0 union all
select 87219,513131,0

declare @表ar table (RELATION_ID int,ASSET_ID int,EVENT_TYPE int)
insert into @表ar
select 494386,87219,1507 union all
select 494386,87219,1507 union all
select 513198,87219,1508 union all
select 513198,87219,1508 union all
select 513131,87219,1507 union all
select 513131,87219,1507

select * from @表a
select * from @表ca
select * from @表ar

我提供下数据,但是我还是没有看懂结果是怎么来的。
叶子 2012-01-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 yhui1989love 的回复:]
以上数据有异常,需要的结果是让表ar变成如下结果:
ASSET_ID APP_LINE_TF_ID APP_LINE_TK_ID
87219 494386 513198
87219 513131 0
[/Quote]
到底是ar表还是ca表?

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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