sql server update inner join on the use of

Suppose we have two tables, a table to store product information Product table with prices listed Price; another table is ProductPrice table, we want to ProductPrice table price fields updated Price Price Price field in the table 80%. 
In Mysql we have several means to do this, one is update table1 t1, table2 ts ... way: 

Copy the code 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 the inner join then update: 

Copy the code 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 the left outer join to do multi-table update, for example if ProductPrice table does not record prices, it will isDeleted field to the Product table 1, the following sql statement: 

Copy the code 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 examples are made between two tables related, but only to update records in a table, in fact, two tables can be updated at the same time, the following sql: 

Copy the code 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' 


dateUpdate two fields associated with the two tables do, update the table of price ProductPrice fields and fields of the Product table.

Guess you like

Origin www.cnblogs.com/baili-luoyun/p/11118588.html