Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

MERGE / UPDATE Query for ORACLE SQL DEVELOPER

I have two different tables, table1 and table2 as shown below

table1

LA_ID ADDRESS CITY STATE
572 A1 C1 S1
300 A1 C1 S1
978 A1 C1 S1
082 A2 C2 S2
026 A2 C2 S2
093 A2 C2 S2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are willing to use just the minimum or maximum value, you can simply use update:

update table_2 t2
    set la_id = (select min(t1.la_id)
                 from table_1 t1 join
                      table_1 t12
                      on t1.state = t12.state and t1.city = t12.city and t1.address = t12.address
                 where t12.la_id = t2.la_id
                )
    where la_id <> (select min(t1.la_id)
                    from table_1 t1 join
                         table_1 t12
                         on t1.state = t12.state and t1.city = t12.city and t1.address = t12.address
                    where t12.la_id = t2.la_id
                   );

I'm not sure how you are choosing 572 and 082 for the specific ids you want when there are duplicates. This would choose 300 and 026.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...