在 mysql 中,update join 语句中包含 order by 会导致 mysql 崩溃吗?
问题
在使用 update join 语句时,加入 order by 会导致 mysql 崩溃。原因不明,求助大神。
建表语句
-- `lebon-infra-dev`.cfg_dev_network_push_status definition create table `cfg_dev_network_push_status` ( `id` bigint not null auto_increment comment '流水号 | 与id为同一字段,自增数列', `task_id` varchar(16) not null comment '任务id', `push_status` char(1) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '0' comment '推送状态(0:待推送/1:已推送/2:已取消)', `push_plan_time` char(14) character set utf8mb4 collate utf8mb4_0900_ai_ci not null comment '预约推送时间(yyyymmddhhmmss)', `push_type` char(1) not null comment '推送种类(1:推送配置项/2:推送优先度/3:推送重启命令)', `dev_id` varchar(32) not null comment '设备id', `config_group_id` varchar(16) not null comment '配置组id', `config_item_id` varchar(16) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '' comment '配置项id', `push_begin_time` char(17) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '00000000000000000' comment '推送开始时间(yyyymmddhhmmssfff)', `push_end_time` char(17) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '00000000000000000' comment '推送结束时间(yyyymmddhhmmssfff)', `push_duration_s` int not null default '0' comment '推送耗时', `push_result` char(1) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '' comment '推送结果0:成功/1:失败', `fail_reason` varchar(128) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '' comment '失败原因', `push_retry_times` int not null default '0' comment '重试次数', `remark` varchar(256) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '' comment '备注', `del_flag` char(1) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '0' comment '删除状态', `version` int not null default '0' comment '版本锁', `create_by` varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci not null comment '创建人', `create_time` datetime not null comment '创建时间', `update_by` varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci default null comment '更新人', `update_time` datetime default null comment '更新时间', primary key (`id`), unique key `cfg_dev_network_push_status_un` (`id`) using btree ) engine=innodb auto_increment=149 default charset=utf8mb4 collate=utf8mb4_0900_ai_ci comment='配置-终端网络配置推送状态'; -- `lebon-infra-dev`.cfg_dev_network_bind_status definition create table `cfg_dev_network_bind_status` ( `id` bigint not null auto_increment comment '任务id | 与id为同一字段,自增数列', `dev_id` varchar(32) not null comment '设备id', `config_group_id` varchar(16) not null comment '配置组id', `config_group_name` varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '' comment '配置组名', `status` char(1) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '0' comment '状态', `push_plan_time` char(14) character set utf8mb4 collate utf8mb4_0900_ai_ci not null comment '预约推送时间(yyyymmddhhmmss)', `push_begin_time` char(17) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '00000000000000000' comment '推送开始时间(yyyymmddhhmmssfff)', `push_end_time` char(17) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '00000000000000000' comment '推送结束时间(yyyymmddhhmmssfff)', `push_duration_s` int not null default '0' comment '推送耗时', `effective_time` char(17) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '00000000000000000' comment '实际生效时间(yyyymmddhhmmssfff)', `invalid_time` char(17) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '00000000000000000' comment '实际失效时间(yyyymmddhhmmssfff)', `fail_reason` varchar(128) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '' comment '失败原因', `cancel_reason` varchar(128) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '' comment '取消原因', `operator` varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '' comment '操作者', `remark` varchar(256) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '' comment '备注', `version` int not null default '0' comment '版本锁', `del_flag` char(1) character set utf8mb4 collate utf8mb4_0900_ai_ci not null default '0' comment '删除状态', `create_by` varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci not null comment '创建人', `create_time` datetime not null comment '创建时间', `update_by` varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci default null comment '更新人', `update_time` datetime default null comment '更新时间', primary key (`id`) using btree, unique key `cfg_dev_network_bind_status_un` (`id`) using btree ) engine=innodb auto_increment=1070 default charset=utf8mb4 collate=utf8mb4_0900_ai_ci comment='配置-终端网络配置绑定状态';
登录后复制
答案
在 mysql 中,update join 语句中不允许包含 order by,否则会报错 incorrect usage of update and order by。
原因
update join 语句的含义是更新加入另一个表中满足条件的行。当使用 order by 时,mysql 无法确定哪个行应该被更新。
解决方案
要避免此错误,可以将 order by 移到子查询或使用笛卡尔乘积:
update cfg_dev_network_push_status set push_retry_times = push_retry_times + 1,
登录后复制