Sql 从Oracle的另一个表更新

Sql 从Oracle的另一个表更新,sql,oracle,sql-update,oracle10g,ora-01427,Sql,Oracle,Sql Update,Oracle10g,Ora 01427,表1 表2 Tripid sequence Pattern 1 1 1 2 1 3 2 1 2 2 我正在尝试更新表1的模式,最终结果应该是: Tripid Pattern 1 A 2 B 我使用的代码: Tripid sequence Pattern 1 1 A 1 2 A 1 3

表1

表2

Tripid  sequence Pattern
  1       1       
  1       2
  1       3
  2       1   
  2       2 
我正在尝试更新表1的模式,最终结果应该是:

Tripid Pattern
  1      A
  2      B
我使用的代码:

Tripid  sequence Pattern
  1       1        A
  1       2        A
  1       3        A
  2       1        B
  2       2        B
Oracle数据库错误:ORA-01427:单行子查询返回多行

如何在oracle 10g中正确执行此操作?

您可以使用此语句

查询

update table1
set table1.pattern = 
    (select pattern from table2 where table1.tripid = table2.tripid) 
where exists 
    (select pattern from table2 where table1.tripid = table2.tripid)
select * from t1
| TRIPID | SEQ | PATTERN |
|--------|-----|---------|
|      1 |   1 |  (null) |
|      1 |   2 |  (null) |
|      1 |   3 |  (null) |
|      2 |   1 |  (null) |
|      2 |   2 |  (null) |
merge into t1
using t2
on (t1.tripid = t2.tripid)
when matched then update
set pattern = t2.pattern
select * from t1

update table1
set table1.pattern = 
    (select pattern from table2 where table1.tripid = table2.tripid) 
where exists 
    (select pattern from table2 where table1.tripid = table2.tripid)
select * from t1
| TRIPID | SEQ | PATTERN |
|--------|-----|---------|
|      1 |   1 |  (null) |
|      1 |   2 |  (null) |
|      1 |   3 |  (null) |
|      2 |   1 |  (null) |
|      2 |   2 |  (null) |
merge into t1
using t2
on (t1.tripid = t2.tripid)
when matched then update
set pattern = t2.pattern
select * from t1
查询

update table1
set table1.pattern = 
    (select pattern from table2 where table1.tripid = table2.tripid) 
where exists 
    (select pattern from table2 where table1.tripid = table2.tripid)
select * from t1
| TRIPID | SEQ | PATTERN |
|--------|-----|---------|
|      1 |   1 |  (null) |
|      1 |   2 |  (null) |
|      1 |   3 |  (null) |
|      2 |   1 |  (null) |
|      2 |   2 |  (null) |
merge into t1
using t2
on (t1.tripid = t2.tripid)
when matched then update
set pattern = t2.pattern
select * from t1
查询

update table1
set table1.pattern = 
    (select pattern from table2 where table1.tripid = table2.tripid) 
where exists 
    (select pattern from table2 where table1.tripid = table2.tripid)
select * from t1
| TRIPID | SEQ | PATTERN |
|--------|-----|---------|
|      1 |   1 |  (null) |
|      1 |   2 |  (null) |
|      1 |   3 |  (null) |
|      2 |   1 |  (null) |
|      2 |   2 |  (null) |
merge into t1
using t2
on (t1.tripid = t2.tripid)
when matched then update
set pattern = t2.pattern
select * from t1

update table1
set table1.pattern = 
    (select pattern from table2 where table1.tripid = table2.tripid) 
where exists 
    (select pattern from table2 where table1.tripid = table2.tripid)
select * from t1
| TRIPID | SEQ | PATTERN |
|--------|-----|---------|
|      1 |   1 |  (null) |
|      1 |   2 |  (null) |
|      1 |   3 |  (null) |
|      2 |   1 |  (null) |
|      2 |   2 |  (null) |
merge into t1
using t2
on (t1.tripid = t2.tripid)
when matched then update
set pattern = t2.pattern
select * from t1

也许tripid在表2中不是唯一的

这在您的示例中,但它在您的真实数据中是唯一的吗

| TRIPID | SEQ | PATTERN |
|--------|-----|---------|
|      1 |   1 |       A |
|      1 |   2 |       A |
|      1 |   3 |       A |
|      2 |   1 |       B |
|      2 |   2 |       B |
返回:

DROP  table table1;
create table table1(tripid number, sequence number, pattern  CHAR(1));
insert into table1 values (1,1,null);
insert into table1 values (1,2,null);
insert into table1 values (1,3,null);
insert into table1 values (2,1,null);
insert into table1 values (2,2,null);

DROP  table table2;
create table table2(tripid number, pattern CHAR(1));
insert into table2 values (1,'A');
insert into table2 values (2,'B');

commit;

select table2.tripid, table2.pattern from table2,table1 where table1.tripid = table2.tripid;


update table1
set table1.pattern = 
    (select pattern from table2 where table1.tripid = table2.tripid) 
where exists 
    (select pattern from table2 where table1.tripid = table2.tripid);

commit;

select * from table1;
这证明了有了好的数据,Oracle(和您的查询)可以完美地工作


Christian

为什么来自另一个表的更新在oracle中不起作用??