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 = r开发者_如何学编程ound(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
.
精彩评论