繁体   English   中英

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

[英]Update multiple rows in MySql from result of a sub-query

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]'), 
  `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`))

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 子句中为更新指定目标表“事件”


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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM