MySQL UPDATE multi-table association update

MySQL can query and update data based on multiple tables. For multi-table UPDATE operations, you need to be cautious. It is recommended to use the SELECT statement to query and verify whether the updated data is consistent with your expectations before updating.
Below we create two tables, one is the product table, which is used to store product information, including the product price field price; the other is the product_price table. Now we need to update the price field price in the product_price table to 80% of the price field price in the product table.
Before the operation, check the data of the two tables separately. The SQL statements and the running results are as follows:

mysql> SELECT * FROM product;
+----+-----------+-----------------------+- ------+----------+
| id | productid | productname | price | isdelete |
+----+-----------+--- --------------------+-------+----------+
| 1 | 1001 | C Language Chinese Net Java Tutorial | 100 | 0 |
| 2 | 1002 | C Language Chinese Network MySQL Tutorial | 110 | 0 |
| 3 | 1003 | C Language Chinese Network Python Tutorial | 120 | 0 |
| 4 | 1004 | C Language Chinese Network C Language Tutorial | 150 | 0 |
| 5 | 1005 | C language Chinese website GoLang tutorial | 160 | 0 |
+----+-----------+----------- ------------+-------+----------+
5 rows in set (0.02 sec)

mysql> SELECT * FROM product_price;
+----+-----------+-------+
| id | productid | price |
+----+-----------+-------+
|  1 |      1001 |  NULL |
|  2 |      1002 |  NULL |
|  3 |      1003 |  NULL |
|  4 |      1004 |  NULL |
|  5 |      1005 |  NULL |
+----+-----------+-------+
5 rows in set (0.01 sec)

The following are several different ways of writing MySQL multi-table update in practice. Execute different SQL statements and carefully observe the changes in the data in the table after the SQL statement is executed. It is easy to understand the usage of multi-table joint update.

1. Use UPDATE

In MySQL, you can use "UPDATE table1 t1,table2,...,table n" to update multiple tables. The SQL statement and the running results are as follows:

mysql> UPDATE product p, product_price pp SET pp.price = p.price * 0.8 WHERE p.productid= pp.productId;
Query OK, 5 rows affected (0.02 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> SELECT * FROM product_price;
+----+-----------+-------+
| id | productid | price |
+----+-----------+-------+
|  1 |      1001 |    80 |
|  2 |      1002 |    88 |
|  3 |      1003 |    96 |
|  4 |      1004 |   120 |
|  5 |      1005 |   128 |
+----+-----------+-------+
5 rows in set (0.00 sec)

2. Via INNER JOIN

Another method is to use INNER JOIN to update multiple tables. The SQL statement is as follows:

mysql> UPDATE product p INNER JOIN product_price pp ON p.productid= pp.productid SET pp.price = p.price * 0.8;
Query OK, 5 rows affected (0.09 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> SELECT * FROM product_price;
+----+-----------+-------+
| id | productid | price |
+----+-----------+-------+
|  1 |      1001 |    80 |
|  2 |      1002 |    88 |
|  3 |      1003 |    96 |
|  4 |      1004 |   120 |
|  5 |      1005 |   128 |
+----+-----------+-------+
5 rows in set (0.00 sec)

3. Via LEFT JOIN

You can also use LEFT JOIN to update multiple tables. If there is no product price record in the product_price table, set the isdelete field of the product table to 1. Add 1006 products to the product table, and not add corresponding information to the product_price table. The SQL statement is as follows.

mysql> UPDATE product p LEFT JOIN product_price pp ON p.productid = pp.productid SET p.isdelete = 1 WHERE pp.productid IS NULL; 
Query OK, 1 row affected (0.04 sec) 
Rows matched: 1 Changed: 1 Warnings: 0 

mysql> SELECT * FROM product; 
+----+-----------+-----------------------+- ------+----------+ 
| id | productid | productname | price | isdelete | 
+----+-----------+--- --------------------+-------+----------+ 
| 1 | 1001 | C Language Chinese Net Java Tutorial | 100 | 0 | 
| 2 | 1002 | C Language Chinese Network MySQL Tutorial | 110 | 0 | 
| 3 | 1003 | C Language Chinese Network Python Tutorial | 120 | 0 | 
| 4 | 1004 | C Language Chinese Network C Language Tutorial | 150 | 0 | 
| 5 | 1005 | C language Chinese website GoLang tutorial | 160 | 0 |
| 6 | 1006 | C Language Chinese Net Spring Tutorial | NULL | 1 | 
+----+-----------+---------------- -------+-------+----------+ 
6 rows in set (0.00 sec)

4. Via subquery

You can also update multiple tables through a subquery. The SQL statement and execution process are as follows:

mysql> UPDATE product_price pp SET price=(SELECT price*0.8 FROM product WHERE productid = pp.productid);
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> SELECT * FROM product_price;
+----+-----------+-------+
| id | productid | price |
+----+-----------+-------+
|  1 |      1001 |    80 |
|  2 |      1002 |    88 |
|  3 |      1003 |    96 |
|  4 |      1004 |   120 |
|  5 |      1005 |   128 |
+----+-----------+-------+
5 rows in set (0.00 sec)

In addition, the above several examples are related to two tables, only update the records in one table. MySQL can also update two tables at the same time. The following statement modifies both tables at the same time.

UPDATE product p INNER JOIN product_price pp ON p.productid= pp.productid SET pp.price = p.price * 0.8, p.dateUpdate = CURDATE()

The two tables are related, and the price field of the product_price table and the dateUpdate of the product table are updated at the same time.

Suppose we have two tables, one table is the Product table to store product information, which contains the product price column Price; the other table is the ProductPrice table, we need to update the price field Price in the ProductPrice table to the price field in the Price table 80%.
In Mysql, we have several means to do this, one is to update table1 t1, table2 ts...:

Copy the code The code is as follows:


UPDATE product p, productPrice pp
SET pp.price = pp.price * 0.8
WHERE p.productId = pp.productId
AND p.dateCreated < '2004-01-01'


Another method is to use inner join and then update:

Copy the code The code is as follows:


UPDATE product p
INNER JOIN productPrice pp
ON p.productId = pp.productId
SET pp.price = pp.price * 0.8
WHERE p.dateCreated < '2004-01-01'


In addition, we can also use left outer join to do multi-table update. For example, if there is no product price record in the ProductPrice table, set the isDeleted field of the Product table to 1, as follows:

Copy the code The code is as follows:


UPDATE product p
LEFT JOIN productPrice pp
ON p.productId = pp.productId
SET p.deleted = 1
WHERE pp.productId IS null


In addition, the above several examples are related to the two tables, but only update the records in one table, in fact, it is possible to update the two tables at the same time, as follows sql:

Copy the code The code is as follows:


UPDATE product p
INNER JOIN productPrice pp
ON p.productId = pp.productId
SET pp.price = pp.price * 0.8,
p.dateUpdate = CURDATE()
WHERE p.dateCreated < '2004-01-01'

The two tables are associated, and the price field of the ProductPrice table and the dateUpdate of the Product table field are updated.

Let's look at a concrete example.

update orders o
    left join users u
        on o.userId = u.id
set o.userName = u.name;

In the above example, the update keyword is followed by a multi-table related result set. MySQL directly treats the multi-table related result set as a single table, and then performs regular update operations on the basis of this single table.

The slight difference with SQL Server is that the set clause of SQL Server follows the specific table to be updated, and the set statement of MySQL follows the result set to be updated (the specific table to be updated is set in the set statement).

 

Guess you like

Origin blog.csdn.net/weixin_42041819/article/details/112986031