前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysql更新同一个表的同个字段

mysql更新同一个表的同个字段

原创
作者头像
用户1571008
发布2022-11-18 10:29:56
2.2K0
发布2022-11-18 10:29:56
举报
文章被收录于专栏:做人如水,做事如山

需求: 数据表express_log的字段option_time,将状态为30的更新为状态为0的加上2秒

代码语言:javascript
复制
EXPLAIN update `express_log` d inner join  (SELECT a.option_time  FROM `express_log` a left join `order` b on a.order_id = b.id where b.created_at > '2022-11-15 17:00:00' and b.transit_center_id in (105,107)  and a.status =0) c on a.order_id = d.order_id set d.option_time = date_add(c.option_time,interval 1 second) and d.order_id in (SELECT a.order_id  FROM `express_log` a left join `order` b on a.order_id = b.id where b.created_at > '2022-11-15 17:00:00' and b.transit_center_id in (105,107)  and a.status = 30) 

报错:

#1054 - Unknown column 'a.order_id' in 'on clause' 原因: 不能先将select出表中的某些值,再update这个表(在同一语句中) 解决:

将查询的数据创建一个临时表去更新同一个表的数据 思路: update  表1  a1  inner join  (select 字段1,字段2 from 表1 where 条件) a2 on 条件

set   a1.字段1 = a2.字段2  最后

代码语言:javascript
复制
update `express_log` d left join (SELECT option_time,order_id FROM `express_log` e left join `order` oo on e.order_id = oo.id where oo.created_at > '2022-11-15 17:00:00' and oo.transit_center_id in (105,107) and oo.express_state >= 60 and e.status =0) c on c.order_id = d.order_id left join `order` o on d.order_id =o.id set d.option_time = date_add(c.option_time, interval 2 second) where o.created_at > '2022-11-15 17:00:00' and o.transit_center_id in (105,107) and o.express_state >= 60 and d.status = 30;

问题解决! 参考: https://blog.csdn.net/wslzjr/article/details/106014855 拓展参考: https://blog.csdn.net/weixin_39618121/article/details/115896835

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档