mysql 联表 更新

mysql 联表 更新,第1张

update t_time t1

set t1.Bks=

(select max(t2.EndTime) from t_diaoyonglian0703 t2

where t1.TraceID=t2.TraceID and t2.Site='bks')-

(select MIN(t2.BeginTime) from t_diaoyonglian0703 t2

where t1.TraceID=t2.TraceID and t2.Site='bks')

where t1.Date='0703'

MySQL:

update B,A set B.ccc=1 where B.bbb=A.aaa

update B set ccc=1 where bbb in(select aaa from A)

这个也可以,不过效率很低的。

在update的时候不能更新select后的结果

示例:

update test.table1 a

set a.name='111'

where a.id = (select max(id) from test.table1)

这个会报错,

可以通过连表解决

update test.table a

join (select max(id) as id from test.table) b

set t.name ='111'

where a.id = b.id

update test.table a ,(select max(id) as id from test.table) b

set a.name ='111'

where a.id = b.id

说明:

update 时,更新的表不能在set和where 中用于子查询

update 时,可以对多个表进行更新

update 后面可以做任意查询,这个作用相当于from


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8702661.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-20
下一篇 2023-04-20

发表评论

登录后才能评论

评论列表(0条)

保存