SQL application: two-table association update update (use one table to update another table)

 #Overview: Use fields in one table to update fields in another table

#Oracle

Preparation Phase

Create 2 tables and insert appropriate data

--创建2个表
create table city(
  code varchar2(3),
  name varchar2(10)
  );

create table people(
  pp_id number(1),
  pp_name varchar2(10),
  city_code varchar2(3),
  city_name varchar2(10)
  );

--插入合适的数据,该语句一次可插入一行数据
insert into city values('001','北京');
insert into city values('002','上海');
insert into city values('003','深圳');
insert into city values('004','南京');
insert into city values('005','广州');
insert into city values('006','成都');
insert into city values('007','重庆');

--该语句一次可插入多行数据
insert all into people values(1,'john','001','北京')
           into people values(2,'timo','002','')
           into people values(3,'张三','003','合肥')
           into people values(4,'李四','008','')
           into people values(5,'王二麻','009','黑龙江')
select * from dual;

The two tables created are as follows:

SQL query

Requirement: Based on the code and name of the city table, update the city_name of people

Method 1: Subquery

--方法1:子查询
update people p 
set city_name = (
                select name 
                from city
                where code = p.city_code);

Conclusion: 1. The city update corresponding to the code corresponds to the correction of errors;

                  2. Cities that are not in the city table are all updated to null in the people table.

Method 2: Subquery (optimization)

update people p 
set city_name = (
                select name 
                from city
                where code = p.city_code)
where exists  (
                select 1 
                from city
                where code = p.city_code);

Conclusion: 1. The city update corresponding to the code corresponds to the correction of errors;

                  2. For cities that are not in the city table, the original data will be retained in the people table and will not be cleared.

Method 3: merge merge data

merge into target table
using source
        on (association condition)
when matched then --data on association
        update
        set target table.column=source table.column, target table.column=source table.column,...
        whee condition
when not matched then --Unrelated data
        insert (target table.column, target table.column,...)
        values ​​(source table.column, source table.column,...)
        where condition;

Note:
1. On (association condition) must have brackets ().
2. Only one of when matched then and when not matched then can be kept.
3. Update where has nothing to do with insert where.
4. Columns in on are not allowed to appear in set.
5. The number of target table.column and source table.column in insert must be equal, and the data types must correspond one to one.

--merge into 并入数据
merge into people p
using city c
    on (p.city_code = c.code)
when matched then
    update
    set p.city_name = c.name;

Conclusion: 1. The city update corresponding to the code corresponds to the correction of errors;

                  2. For cities that are not in the city table, the original data will be retained in the people table and will not be cleared.

Resolved issue: ORA-01427: single row subquery returns multiple rows

insert into city values('002','大西北');

When there are two identical codes in the city table, corresponding to different cities, and any city needs to be selected for update

update people p 
set city_name = (
                select max(name)
                from city
                where code = p.city_code)
where exists  (
                select 1 
                from city
                where code = p.city_code);

--或者

merge into people p
using (select c.code ,max(c.name) name2
      from city c
      group by c.code ) cc
     on (p.city_code = cc.code)
when matched then
     update
     set p.city_name = cc.name2;
  • The above writing method usingconstructs a new cc table later, but the name must be processed. If it is a varchar type, you can choose max, min and other functions. If it is a number type, you can use sum, avg and other functions. In short, you must The name is processed (when there are multiple corresponding ones, which one should be used? The largest or the smallest one). The new cc is that one code corresponds to one name.

Conclusion: 1. The city update corresponding to the code corresponds to the correction of errors;

                  2. If a code corresponds to multiple city names, update one of them;

                  3. For cities that are not in the city table, the original data will be retained in the people table and will not be cleared.

Solve the problem: related updates between two tables, updating multiple columns at the same time

update customers a   -- 使用别名
set    (city_name,customer_type)=(select b.city_name,b.customer_type 
                                  from   tmp_cust_city b 
                                  where  b.customer_id=a.customer_id)
where  exists (select 1 
               from   tmp_cust_city b
               where  b.customer_id=a.customer_id);

--或者

merge into customers a
using tmp_cust_city b
    on (a.customer_id = b.customer_id)
when matched then
    update
    set a.city_name = b.city_name,a.customer_type = b.customer_type;

#MySQL

update people p, city c 
set p.city_name = c.name
where p.city_code = c.code;

Conclusion: 1. The code corresponds to the city update and the corresponding error correction;

           2. For cities that are not in the city table, the original data will be retained in the people table and will not be cleared.

update people p left join city c
                       ON p.city_code=c.`code` 
SET p.city_name=c.`name`;

Conclusion: 1. The city update corresponding to the code corresponds to the correction of errors;

                  2. Cities that are not in the city table are all updated to null in the people table.

In fact, update can be divided into outer connections and inner connections. You must think clearly before using them, otherwise the data in the main table will be cleared.

#SqlServer

update A
set A1=B.B1,A2=B.B2,A3=B.B3,A4=B.B4
from A,B
where A.AID=B.BID

reference:

How to use data from one table to update data from another table in Oracle - kangkaii - Blog Park (cnblogs.com)

mySQL: SQL statement for updating two tables (using one table to update another table)_Update statement two tables-CSDN Blog

How does the UPDATE statement associate two tables-CSDN Blog

Guess you like

Origin blog.csdn.net/qq_41961171/article/details/134361369