P粉2371257002023-08-28 13:59:47
You can do 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
P粉4818158972023-08-28 13:51:38
The problem is that, for whatever stupid reason, MySQL doesn't let you write a query like this:
UPDATE myTable SET myTable.A = ( SELECT B FROM myTable INNER JOIN ... )
That is, if you want to perform UPDATE
/INSERT
/DELETE
operations on the table, you cannot query internally (but you Can reference fields in external tables...)
The solution is to replace the myTable
instance in the subquery with (SELECT * FROM myTable)
as shown below
UPDATE myTable SET myTable.A = ( SELECT B FROM (SELECT * FROM myTable) AS something INNER JOIN ... )
This will obviously cause the necessary fields to be implicitly copied into the temporary table, so this is allowed.
I found this solution here. Notes for this article: