Mysql Update multiple table update SQL statement Summary _mysql

Source: Internet
Author: User
Tags mysql update one table
Suppose we have two tables, a table that holds product information for the product table, which contains price list prices; Another table is the Productprice table, and we want to update the Price field in the Productprice table to 80% of the price field in the prices table.
In MySQL we have several means to do this, one is update table1 t1, table2 ts ... The way:
Copy Code code 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 approach is to use the inner join and then update:
Copy Code code 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 use the left outer join to do more table update, for example, if there is no product price record in the Productprice table, the isdeleted field of the product table is set to 1, the following SQL statement:
Copy Code code 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 examples are related to two tables, but only update the records in one table, in fact, you can update two tables, the following SQL:
Copy Code code 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 '

Two tables are associated, updating the Price field of the Productprice table and the Dateupdate two fields of the Product table field.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.