MYSQL update with WHERE SELECT subquery error

后端 未结 4 1306
时光取名叫无心
时光取名叫无心 2020-12-05 02:46

I have an issue with getting select sub-queries to work on an UPDATE. I\'m trying something like the following:

UPDATE foo
   SET bar=bar-1
 WHE         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-05 02:56

    From this web article

    The reason for this error is that MySQL doesn’t allow updates to a table when you are also using that same table in an inner select as your update criteria. The article goes on to provide a solution, which is to use a temporary table.

    Using this example, your update should be this:

    update foo
    set bar = bar - 1
    where baz in
    (
      select baz from
      (
        select baz
        from foo
        where fooID = '1'
      ) as arbitraryTableName
    )
    

提交回复
热议问题