You can't specify target table for update in FROM clause

前端 未结 11 1523
野趣味
野趣味 2020-11-21 22:46

I have a simple mysql table:

CREATE TABLE IF NOT EXISTS `pers` (
  `persID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(35) NOT NULL,
  `gehalt` int(11         


        
相关标签:
11条回答
  • 2020-11-21 23:06

    Other workarounds include using SELECT DISTINCT or LIMIT in the subquery, although these are not as explicit in their effect on materialization. this worked for me

    as mentioned in MySql Doc

    0 讨论(0)
  • 2020-11-21 23:07

    The problem is that MySQL, for whatever inane reason, doesn't allow you to write queries like this:

    UPDATE myTable
    SET myTable.A =
    (
        SELECT B
        FROM myTable
        INNER JOIN ...
    )
    

    That is, if you're doing an UPDATE/INSERT/DELETE on a table, you can't reference that table in an inner query (you can however reference a field from that outer table...)


    The solution is to replace the instance of myTable in the sub-query with (SELECT * FROM myTable), like this

    UPDATE myTable
    SET myTable.A =
    (
        SELECT B
        FROM (SELECT * FROM myTable) AS something
        INNER JOIN ...
    )
    

    This apparently causes the necessary fields to be implicitly copied into a temporary table, so it's allowed.

    I found this solution here. A note from that article:

    You don’t want to just SELECT * FROM table in the subquery in real life; I just wanted to keep the examples simple. In reality, you should only be selecting the columns you need in that innermost query, and adding a good WHERE clause to limit the results, too.

    0 讨论(0)
  • 2020-11-21 23:18

    MySQL doesn't allow selecting from a table and update in the same table at the same time. But there is always a workaround :)

    This doesn't work >>>>

    UPDATE table1 SET col1 = (SELECT MAX(col1) from table1) WHERE col1 IS NULL;
    

    But this works >>>>

    UPDATE table1 SET col1 = (SELECT MAX(col1) FROM (SELECT * FROM table1) AS table1_new) WHERE col1 IS NULL;
    
    0 讨论(0)
  • 2020-11-21 23:20

    You can make this in three steps:

    CREATE TABLE test2 AS
    SELECT PersId 
    FROM pers p
    WHERE (
      chefID IS NOT NULL 
      OR gehalt < (
        SELECT MAX (
          gehalt * 1.05
        )
        FROM pers MA
        WHERE MA.chefID = p.chefID
      )
    )
    

    ...

    UPDATE pers P
    SET P.gehalt = P.gehalt * 1.05
    WHERE PersId
    IN (
      SELECT PersId
      FROM test2
    )
    DROP TABLE test2;
    

    or

    UPDATE Pers P, (
      SELECT PersId
      FROM pers p
      WHERE (
       chefID IS NOT NULL 
       OR gehalt < (
         SELECT MAX (
           gehalt * 1.05
         )
         FROM pers MA
         WHERE MA.chefID = p.chefID
       )
     )
    ) t
    SET P.gehalt = P.gehalt * 1.05
    WHERE p.PersId = t.PersId
    
    0 讨论(0)
  • 2020-11-21 23:22

    In Mysql, you can not update one table by subquery the same table.

    You can separate the query in two parts, or do

     UPDATE TABLE_A AS A
     INNER JOIN TABLE_A AS B ON A.field1 = B.field1
     SET field2 = ? 
    0 讨论(0)
提交回复
热议问题