免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7841 | 回复: 3
打印 上一主题 下一主题

求助多表关联update的用法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-23 11:26 |只看该作者 |倒序浏览
informix数据库中有两个表:table1:name ,value1,value2, state,
table2:name,value1,value2,state
现在要将table2表中state为1的记录的value1,value2的值更新到table1中相同name的记录上,update语句应该如何写?
请各位指点,我写了好久都不成功,老是报语法错误201。

论坛徽章:
0
2 [报告]
发表于 2006-03-23 12:11 |只看该作者
按name 做唯一定位 ,试试这个
update table1 set
value1 = (select value1  from table2
      where table2.name = table1.name  ),
value2 =  (select value2 from table2
      where table2.name = table1.name  )

where name in ( select name from table2 where state =1 );

[ 本帖最后由 wenlq 于 2006-3-23 12:16 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2006-03-23 13:21 |只看该作者

回复 2楼 wenlq 的帖子

谢谢!受益非浅,我把state条件加到set的子查询里了,难怪不行

论坛徽章:
0
4 [报告]
发表于 2006-03-23 16:25 |只看该作者
set pdqpriority high;

select --+use_hase(a,b) a.name,( case b.value1 when null then a.value1 else b.value1 ) as value1,
( case b.value2 when null then a.value2 else b.value2 ) as value2,a.state
from table1 a,table2 b
where a.state = 1
and a.name = b.name
into temp tmp_table1;

drop table table1;
create table1(....);

insert into table1 select * from tmp_table1;

对于数据量较大的表,最好把PDQ加上去,先往临时表中写数据,然后再回写表。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP