avatar
how to make this query# Database - 数据库
r*e
1
I have two relations A(a1,a2), B(b1,b2). I want to update the value of b2 to
a2 if a1 and b1 has the same value. How to write a query?
avatar
s*n
2
update B
set B.b2=A.a2
from A,B
where A.a1=B.b1

【在 r***e 的大作中提到】
: I have two relations A(a1,a2), B(b1,b2). I want to update the value of b2 to
: a2 if a1 and b1 has the same value. How to write a query?

avatar
r*e
3
It's not correct, I tried in ORACLE, it does not support update sth from
tables.
Does anyone know the answer?

to

【在 s******n 的大作中提到】
: update B
: set B.b2=A.a2
: from A,B
: where A.a1=B.b1

avatar
s*n
4
Hehe, it works on sql server.

【在 r***e 的大作中提到】
: It's not correct, I tried in ORACLE, it does not support update sth from
: tables.
: Does anyone know the answer?
:
: to

avatar
s*n
5
Try this with Oracle:
update B
set B.b2= (select A.a2 from A where A.a1=B.b1)
在 swingfan (狮子座小马的爸爸) 的大作中提到: 】
avatar
r*e
6
Acturally, I tried this before, it only works when you have unique a2 values.

【在 s******n 的大作中提到】
: Try this with Oracle:
: update B
: set B.b2= (select A.a2 from A where A.a1=B.b1)
: 在 swingfan (狮子座小马的爸爸) 的大作中提到: 】

avatar
s*n
7
en. u mean the subquery must return one row only. sigh... the sql server
syntax makes more sense, wonder what oracle is thinking ...

【在 r***e 的大作中提到】
: Acturally, I tried this before, it only works when you have unique a2 values.
avatar
k*h
8
try this explicit cursor in loop:
/
BEGIN
FOR B in (select * from B)
LOOP
BEGIN
UPDATE A
set A.a2=(B.b2)
where A.a1=B.B1
END;
END LOOP;
END;
/

values.

【在 s******n 的大作中提到】
: en. u mean the subquery must return one row only. sigh... the sql server
: syntax makes more sense, wonder what oracle is thinking ...

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。