Home > Database > Mysql Tutorial > MySql多表关联Update更新sql语句

MySql多表关联Update更新sql语句

WBOY
Release: 2016-06-07 17:53:13
Original
1943 people have browsed it

我们用到最多的update更新数据都是单表更新了,但有的时候我们不得不使用关联多表进行数据更新了,下面我给各位介绍利用upate实现多表关联更新。

对单表执行更新没有什么好说的,无非就是update table_name set col1 = xx,col2 = yy where col = zz,主要就是where条件的设置。有时候更新某个表可能会涉及到多张数据表,例如:

 代码如下 复制代码

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

其实update也可以用到left join、inner join来进行关联,可能执行效率更高,把上面的sql替换成join的方式如下:

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


mysql关联多表进行update更新操作

 代码如下 复制代码

UPDATE Track
INNER JOIN MV
ON Track.trkid=MV.mvid
SET Track.is_show=MV.is_show
WHERE trkid


等同于


UPDATE Track,MV
SET Track.is_show=MV.is_show
WHERE Track.trkid=MV.mvid and trkid

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template