如何解决根据子查询的结果更新MySql中的多行
update `event` set
`name` = concat(`name`,' [soft deleted]'),`domain` = concat(`domain`,' [soft deleted]')
where `name` regexp 'portability reference|created new portability|new portability created|sent postpaid portability initiated sms|init new postpaid portability process verification sms|created new postpaid portability in masmovil|created new prepaid portability in masmovil'
and `pid` in ('801413','794854')
为什么打击查询不起作用,上述查询为什么起作用?
update `event` set
`name` = concat(`name`,' [soft deleted]')
where `name` regexp 'portability reference|created new portability|new portability created|sent postpaid portability initiated sms|init new postpaid portability process verification sms|created new postpaid portability in masmovil|created new prepaid portability in masmovil'
and `pid` in (select distinct `pid` from `event` where (`name` = 'created new postpaid portability in masmovil' or `name` = 'created new prepaid portability in masmovil') and not JSON_LENGTH(`data`))
update `event` set
`name` = concat(`name`,' [soft deleted]')
where `name` regexp 'portability reference|created new portability|new portability created|sent postpaid portability initiated sms|init new postpaid portability process verification sms|created new postpaid portability in masmovil|created new prepaid portability in masmovil'
and `pid` = ANY (select distinct `pid` from `event` where (`name` = 'created new postpaid portability in masmovil' or `name` = 'created new prepaid portability in masmovil') and not JSON_LENGTH(`data`))
错误1093(HY000):您无法为更新指定目标表“事件” 在FROM子句
中
mysql> describe event;
+-----------+---------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+-------------------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| timestamp | timestamp | NO | MUL | CURRENT_TIMESTAMP | |
| pid | varchar(30) | NO | MUL | NULL | |
| domain | varchar(50) | NO | MUL | NULL | |
| name | varchar(200) | NO | MUL | NULL | |
| data | json | YES | | NULL | |
+-----------+---------------------+------+-----+-------------------+----------------+
解决方法
MySQL不支持在子查询中重新使用更新的表。您可以改用联接:
update `event` e
inner join (
select distinct `pid`
from `event`
where
(`name` = 'created new postpaid portability in masmovil' or `name` = 'created new prepaid portability in masmovil')
and not JSON_LENGTH(`data`)
) x on x.pid = e..pid
set
`name` = concat(`name`,' [soft deleted]'),`domain` = concat(`domain`,' [soft deleted]')
where `name` regexp 'portability reference|created new portability|new portability created|sent postpaid portability initiated sms|init new postpaid portability process verification sms|created new postpaid portability in masmovil|created new prepaid portability in masmovil'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。