简体   繁体   中英

Weird: can't specify target table for update in FROM clause

I don't understand why my query runs fine in phpMyAdmin and in my php script it gives me the message: can't specify target table for update in FROM clause !!!

update tableA set 
Field1 = round(round((select (select br.FieldA from tableB br where tableA.id=br.id and tableA.id2=br.id2) as x),4) - round(tableA.FieldA,4),4)

That is correct. You cannot select in the nested query the same table you're updating now.

The better solution will be to use joins in your update. http://dev.mysql.com/doc/refman/5.5/en/update.html

This should work with multi-table update:

UPDATE tableA, tableB
SET tableA.Field1 = round(round(tableB.FieldA, 4) - round(tableA.FieldA,4), 4)
WHERE tableA.id=tableB.id AND tableA.id2=tableB.id2;

Search for join in http://dev.mysql.com/doc/refman/5.0/en/update.html

you can do it using multiple copies of the same table like this:

UPDATE tablename, tablename b 
SET tablename.col1 = val 
WHERE tablename.col1 = b.col1 AND b.col2 IN (val1,val2,val3);

The val in the above query can also be a variable like b.col2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM