mysql 使用相同表更新选择查询引发错误

af7jpaap  于 2022-11-21  发布在  Mysql
关注(0)|答案(2)|浏览(179)
UPDATE
   work_info as info1
 SET
   info1.status_id = 1
  WHERE
 info1.info_id IN(
    SELECT info2.info_id
    FROM work_info as info2
      WHERE info2.info_id IN (
        SELECT MAX(info3.info_id)
          FROM work_info as info3
       GROUP BY info3.user_license_id)
    AND info2.status_id = 5)

获取此错误#1093 -您不能在FROM子句中指定要更新目标表'info 1'

t9aqgxwy

t9aqgxwy1#

直接(不尝试理解查询的逻辑):

UPDATE work_info as info1
JOIN work_info as info2 USING (info_id)
JOIN ( SELECT MAX(info_id) info_id
       FROM work_info
       GROUP BY user_license_id 
       ) as info3 USING (info_id)
SET info1.status_id = 1
WHERE info2.status_id = 5;
s4n0splo

s4n0splo2#

UPDATE table SET a=value WHERE x IN
    (SELECT x FROM table WHERE condition);

因为它是同一张表,所以您可以进行以下操作:

UPDATE table SET a=value WHERE x IN
    (SELECT * FROM (SELECT x FROM table WHERE condition) as t)

[更新或删除或其他]
如果你想在一个表中指定一个目标表,那么你可以在一个表中指定一个目标表,然后在另一个表中指定一个目标表。

相关问题