当前位置:网站首页>MySQL跨表、多表更新SQL语句总结

MySQL跨表、多表更新SQL语句总结

2022-08-09 19:56:00 全栈程序员站长

大家好,又见面了,我是你们的朋友全栈君。

单表更新
UPDATE table_name SET field1=new-value1, field2=new-value2 WHERE field=value
多表更新

情况一:

UPDATE userA as a , userB as b SET a.num = b.num*0.5 WHERE a.id = b.uid AND a.deleted_at is null;
UPDATE userA as a INNER JOIN userB as b ON a.id = b.uid SET a.num = b.num*0.5 WHERE AND a.deleted_at is null;

情况二:

UPDATE userA as a INNER JOIN userB as b ON a.id = b.uid SET a.num = b.num*0.5,a.state = 1 WHERE a.id = b.id 
AND a.deleted_at is null
AND (
	b.gb_name = 'a'
	OR b.gb_name = 'b'
	OR b.gb_name = 'c'
	OR b.gb_name = 'd'
	OR b.gb_name = 'e'
	OR b.gb_name = 'f'
	OR b.gb_name = 'g'
);

扩展: inner join和where区别: 1 WHERE子句中使用的连接语句,在数据库语言中,被称为隐性连接。INNER JOIN……ON子句产生的连接称为显性连接。(其他JOIN参数也是显性连接)WHERE和INNER JOIN产生的连接关系,没有本质区别,结果也一样。但是!隐性连接随着数据库语言的规范和发展,已经逐渐被淘汰,比较新的数据库语言基本上已经抛弃了隐性连接,全部采用显性连接了。

2,测试结果inner join效率更快 事例: Inner jion 原理:

SELECT
	*
FROM
	A
INNER JOIN B ON B.ID = A.ID
AND B.State = 1
INNER JOIN C ON B.ID = C.ID

where 原理:

SELECT
	*
FROM
	A
INNER JOIN B ON B.ID = A.ID
INNER JOIN C ON B.ID = C.ID
WHERE
	B.State = 1

INNER JOIN时会产生一个结果集,WHERE条件在这个结果集中再根据条件进行过滤。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/105820.html原文链接:https://javaforall.cn

原网站

版权声明
本文为[全栈程序员站长]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/2069143