有两个表 a、b ,其中表a中有字段a1、a2 ,表b中有字段b1、b2要求实现当a2=b2时 把b1的值赋给a1在pl/sql中执行语句:update a set a.a1 = (select b.b1 from b where a.a2=b.b2) 报错:单行子查询返回对于一个行请求帮助
解决方案 »
- 新手求解一个简单的问题:如何在一个语句块里面调用存储过程?
- 2张表的比对
- SQL优化问题
- update效率问题
- DBA平时应该注意数据库的哪些问题呢?
- 我最近在面试,请教各位大神说一些数据库的知识。谢谢了
- Orale 连接查询顺序
- 怎么样重命名表名,列名?
- [求助]求电子书籍《Oracle 9i 初者者指南》的下载地址
- 空字段在查询中遇到的问题??
- oracle 11g :报错为ORA-01033 ORACLE initialization or shutdown in progress
- win7(64)plsql连接的时候提示‘ORA-12154:TNS:无法解析指定的连接标志符’;的解决方法
select b.b1 from b where b.b2=a.a2)
(select b1 from
(select b.b1, b.b2
from b where b.b2 in
(select b2 from b group by b2 having count(b2) = 1)
union all
select b.b1, b.b2
from b where b.b2 in
(select b2 from b group by b2 having count(b2) > 1)
and rowid in (select min(rowid)
from b group by b2
having count(b2) > 1)) c where c.b1=a.a1) //查询出不重复的数据,如果重复就取一条
update a set a.a1 = (select b.b1 from b where a.a2=b.b2)中的select b.b1 from b where a.a2=b.b2 有可能是多个字段,你把多个字段赋给了a.al
产生问题的原因是表A的一条记录对应表B中的多条记录,在执行更新语句时数据库无法判断应当使用B中哪条记录的值去更新表A的数据。所以需要更改SQL在其中加上挑选值的操作,例句如下:-- 如果有多条记录,使用最小的值
update a set a.a1 = (select min(b.b1) from b where a.a2=b.b2);-- 如果有多条记录,使用最大的值
update a set a.a1 = (select max(b.b1) from b where a.a2=b.b2);-- 如果有多条记录,使用平均值
update a set a.a1 = (select adv(b.b1) from b where a.a2=b.b2);-- 如果有多条记录,使用第1条记录的值
update a set a.a1 = (select b.b1 from b where a.a2=b.b2 and rownum = 1)