表被指定两次,既作为 'UPDATE' 的目标,也作为 mysql 中数据的单独源

Table is specified twice, both as a target for #39;UPDATE#39; and as a separate source for data in mysql(表被指定两次,既作为 UPDATE 的目标,也作为 mysql 中数据的单独源)
本文介绍了表被指定两次,既作为 'UPDATE' 的目标,也作为 mysql 中数据的单独源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 mysql 中有以下查询,我想检查 branch idyearfinance 类型是否来自 branch_mastermanagerbranch idyear 相等,然后更新状态manager 表对 manager 中的 branch id

I have below query in mysql where I want to check if branch id and year of finance type from branch_master are equal with branch id and year of manager then update status in manager table against branch id in manager

UPDATE manager as m1 
  SET m1.status = 'Y'
  WHERE m1.branch_id IN (
    SELECT m2.branch_id FROM manager as m2 
     WHERE (m2.branch_id,m2.year) IN (
        (
          SELECT DISTINCT branch_id,year 
            FROM `branch_master` 
           WHERE type = 'finance'
        )
     )
  )

但出现错误

表 'm1' 被指定两次,既作为 'UPDATE' 的目标,也作为单独的数据来源

Table 'm1' is specified twice, both as a target for 'UPDATE' and as a separate source for data

推荐答案

这是一个典型的 MySQL 事情,通常可以通过从表中选择来规避,即代替

This is a typical MySQL thing and can usually be circumvented by selecting from the table derived, i.e. instead of

FROM manager AS m2

使用

FROM (select * from manager) AS m2

完整声明:

UPDATE manager
SET status = 'Y'
WHERE branch_id IN
(
  select branch_id
  FROM (select * from manager) AS m2
  WHERE (branch_id, year) IN
  (
    SELECT branch_id, year
    FROM branch_master
    WHERE type = 'finance'
  )
);

这篇关于表被指定两次,既作为 'UPDATE' 的目标,也作为 mysql 中数据的单独源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)