使用连接更新oracle SQL中的记录并且存在查询

时间:2017-05-24 13:23:51

标签: sql oracle join

我有下表和

  1. tempaddresstable
  2. 帐户
  3. 前提
  4. 地址
  5. 以下是详细信息的链接(帐户ID =前提的account_fk) (前提的address_fk =地址的id)

    所以唯一的目标是将所有记录信息从tempaddresstable更新到地址表

    我已尝试过以下查询,但收到错误

    UPDATE ADDRESS AD
    SET AD.ADDRESS1 =
      (SELECT (B.TEMPADDRESS1)
       FROM tempAddressTable B,
            ADDRESS AD
       WHERE AD.ID =
           (SELECT (P.ADDRESS_FK)
            FROM ACCOUNT A
            LEFT JOIN tempAddressTable B ON A.EXT_REF = B.TEMPEXTREF
            LEFT JOIN PREMISE P ON A.ID = P.ACCOUNT_FK
            LEFT JOIN ADDRESS AD ON AD.ID = P.ADDRESS_FK
            WHERE A.EXT_REF = B.TEMPEXTREF ))
    WHERE EXISTS
        (SELECT 1
         FROM tempAddressTable B
         WHERE AD.ID =
             (SELECT DISTINCT max(P.ADDRESS_FK)
              FROM ACCOUNT A
              LEFT JOIN PREMISE P ON A.ID = P.ACCOUNT_FK
              LEFT JOIN tempAddressTable B ON A.EXT_REF = B.TEMPEXTREF
              WHERE A.EXT_REF = B.TEMPEXTREF ));
    

    错误:

    SQL错误:ORA-01427:单行子查询返回多行 01427. 00000 - "单行子查询返回多行" *原因:
    *操作:

    表格详情

    1. tempaddresstable
    2. 帐户
    3. 前提
    4. 地址
    5. tempaddresstable

      +------------+----------------------------+
      | TEMPEXTREF | TEMPADDRESS1               |
      +------------+----------------------------+
      | 34         | 101 DRCSG117432RES RD TEST |
      | 35         | 102 DRCSG117               |
      | 36         | 100 DRCSG117432RES RD      |
      +------------+----------------------------+
      

      帐户

      +---------+---------+
      | ID      | EXT_REF |
      +---------+---------+
      | 1041261 | 34      |
      | 1041262 | 35      |
      | 1041263 | 36      |
      +---------+---------+
      

      前提

      +---------+------------+------------+
      | ID      | ACCOUNT_FK | ADDRESS_FK |
      +---------+------------+------------+
      | 1044610 | 1041261    | 1041502    |
      | 1044611 | 1041262    | 1041503    |
      | 1044612 | 1041263    | 1041504    |
      +---------+------------+------------+
      

      地址

      +---------+----------+
      | ID      | ADDRESS1 |
      +---------+----------+
      | 1041502 | test     |
      | 1041503 | test     |
      | 1041504 | test     |
      +---------+----------+
      

1 个答案:

答案 0 :(得分:0)

它不适合你吗?

drop table tempaddresstable;
create table tempaddresstable (tempextref number, tempaddress1 varchar2(100));
insert into tempaddresstable values (34, '101 DRCSG117432RES RD TEST');
insert into tempaddresstable values (35, '102 DRCSG117');
insert into tempaddresstable values (36, '100 DRCSG117432RES RD');
commit;

drop table accounts;
create table accounts (id number, ext_ref number);
insert into accounts values (1041261 , 34);
insert into accounts values (1041262 , 35);
insert into accounts values (1041263 , 36);
commit;

drop table premise;
create table premise (id number, account_fk number, address_fk number);
insert into premise values (1044610 ,1041261 ,1041502);
insert into premise values (1044611 ,1041262, 1041503);
insert into premise values (1044612 ,1041263, 1041504);
commit;

drop table address;
create table address (id number, address1 varchar2(100));
insert into address values (1041502, 'test');
insert into address values (1041503, 'test');
insert into address values (1041504, 'test');
commit;


update address a set a.address1 = 
(
with results as 
(select p.address_fk,t.tempaddress1
from accounts aa join tempaddresstable t on aa.ext_ref = t.tempextref join 
premise p on p.account_fk = aa.id  )
select tempaddress1 from results r
where r.address_fk = a.id);

commit;
  

我承诺犯了更多错误,所以请在生产中实施之前测试我的所有解决方案。最诚挚的问候,阿德南。