update垮表条件查询更新求教

[复制链接]
查看11 | 回复8 | 2007-2-7 08:25:32 | 显示全部楼层 |阅读模式
a表对应关系: olduserid,newuserid两个字段
b表新的表:newuserid,passwd
c表旧的表: olduserid,passwd
现在要通过a表的ID对应关系把,c表的密码数据直接按ID更新到b表中...一个语句实现


回复

使用道具 举报

千问 | 2007-2-7 08:25:32 | 显示全部楼层
什么叫“c表的数据直接按ID更新到b表中”呀??
难道:
insert into b select a.newuserid,c.passwd from a,c where a.olduserid=c.olduserid
回复

使用道具 举报

千问 | 2007-2-7 08:25:32 | 显示全部楼层
update b set b.passwd =(select c.passwd from a,c

where a.olduserid=c.olduserid

anda.newuserid = b.newuserid)
---------------------------
这个意思??? 这样更新可能有的会变空,可以加条件
回复

使用道具 举报

千问 | 2007-2-7 08:25:32 | 显示全部楼层
update (selectb.passwd,b.olduserid,a.olduserid tt from b,a where a.newuserid = b.newuserid) x
set x.passwd =(select c.passwd from c
where
x.tt= c.olduserid)
好像在那儿看到过可以这么写!不知道可以不,帮忙求证一下!
---------------------------
回复

使用道具 举报

千问 | 2007-2-7 08:25:32 | 显示全部楼层
最初由 piliskys 发布
[B]update b set b.passwd =(select c.passwd from a,c

where a.olduserid=c.olduserid

anda.newuserid = b.newuserid)
---------------------------
这个意思??? 这样更新可能有的会变空,可以加条件 [/B]

加个where exists条件
回复

使用道具 举报

千问 | 2007-2-7 08:25:32 | 显示全部楼层
怎么加where exists条件?
回复

使用道具 举报

千问 | 2007-2-7 08:25:32 | 显示全部楼层
我觉得piliskys的算法再加上WHERE EXISTS 语句的话应该是比较理想的。
回复

使用道具 举报

千问 | 2007-2-7 08:25:32 | 显示全部楼层
最初由 myfriend2010 发布
[B]update (selectb.passwd,b.olduserid,a.olduserid tt from b,a where a.newuserid = b.newuserid) x
set x.passwd =(select c.passwd from c
where
x.tt= c.olduserid)
好像在那儿看到过可以这么写!不知道可以不,帮忙求证一下!
--------------------------- [/B]

sql>create table a(olduserid number(3),newuserid number(3));
表已创建。
sql>create table b(newuserid number(3),passwd varchar2(10));
表已创建。
sql>create table c( olduserid number(3),passwd varchar2(10));
表已创建。
sql>update (select b.passwd,b.olduserid,a.olduserid tt from b,a where a.newuseri
d = b.newuserid) x
2set x.passwd =(select c.passwd from c where x.tt= c.olduserid);
update (select b.passwd,b.olduserid,a.olduserid tt from b,a where a.newuserid =
b.newuserid) x

*
ERROR 位于第 1 行:
ORA-00904: "B"."OLDUSERID": invalid identifier

sql>update (select b.passwd,b.newuserid,a.olduserid tt from b,a where a.newuseri
d = b.newuserid) x
2set x.passwd =(select c.passwd from c where x.tt= c.olduserid);
set x.passwd =(select c.passwd from c where x.tt= c.olduserid)
*
ERROR 位于第 2 行:
ORA-01779: cannot modify a column which maps to a non key-preserved table

sql>
所以结果是不行。
回复

使用道具 举报

千问 | 2007-2-7 08:25:32 | 显示全部楼层
updateb set passwd = (select passwd from
(select olduserid,newuserid,passwd from a,c where a.olduserid = c.olduserid) a1
where b.newuserid = a1.newuserid )
where exists ( select 1 from a where a.newuserid = b.newuserid);
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行