首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从子查询的结果中更新MySql中的多行

从子查询的结果中更新MySql中的多行
EN

Stack Overflow用户
提问于 2020-10-16 11:46:39
回答 1查看 33关注 0票数 0
代码语言:javascript
复制
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')

当blow查询不工作时,为什么要执行上述查询?

代码语言:javascript
复制
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 (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`))

代码语言:javascript
复制
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` = 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子句中进行更新

代码语言:javascript
复制
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              |                |
+-----------+---------------------+------+-----+-------------------+----------------+
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-16 11:49:34

MySQL不支持在子查询中重用更新的表。您可以使用一个join代替:

代码语言:javascript
复制
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'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64388565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档