MySQL association table update data SQL script

 

Suppose we have two tables, one is the Product table to store product information, which has the product price column Price; the other is the ProductPrice table, we want 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 the way of update table1 t1, table2 ts ...:

 

UPDATE product p, productPrice pp 

SET pp.price = p.price * 0.8 

WHERE p.productId = pp.productId 

AND p.dateCreated < '2004-01-01' 

 

Another way is to use inner join and then update: 

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 update multiple tables. 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: 

 

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 the records in one table are updated. In fact, two tables can be updated at the same time, as follows sql: 

 

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 field of the Product table field are updated.

 

 

update table_1 set score = score + 5 where uid in (select uid from table_2 where sid = 10);

In fact, update can also be associated with left join and inner join, which may be more efficient. The way to replace the above sql with join is as follows:

update table_1 t1 inner join table_2 t2 on t1.uid = t2.uid set score = score + 5 where t2.sid = 10;

 

 

MySQL associates multiple tables for update update operation

 The code is as follows

UPDATE Track

INNER JOIN MV

ON Track.trkid=MV.mvid

SET Track.is_show=MV.is_show

WHERE trkid<6

 

Equivalent to

 

UPDATE Track,MV

SET Track.is_show=MV.is_show

WHERE Track.trkid=MV.mvid and trkid<6

 

 

update student s, city c

   set s.city_name = c.name

 where s.city_code = c.code;

You can also try the following correlated subquery:

 

update student s set city_name = (select name from city where code = s.city_code);

 

============================================================

eg

 

update t_xd_after_loan_core a, t_xd_loan_extend e 

set a.bid_limit = e.bid_limit,a.bid_borrow_period_unit=e.bid_borrow_period_unit 

where a.bid =e.bid and a.create_date < '2017-06-22 22:00:00' and a.play_money_type in (1,3,10); 

 

update t_xd_after_loan_core a 

set a.bid_limit=1,a.bid_borrow_period_unit=2 

where a.create_date < '2017-06-22 22:00:00' and a.play_money_type in (5,6) and a.refund_way=5; 

 

update t_xd_after_loan_core a, t_xd_loan_extend e 

set a.bid_limit = e.bid_limit,a.bid_borrow_period_unit=e.bid_borrow_period_unit 

where a.bid =e.bid and a.create_date < '2017-06-22 22:00:00' and a.play_money_type in (5,6) and a.refund_way=4; 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326215694&siteId=291194637