akitchin Posted October 11, 2006 Share Posted October 11, 2006 after some sussing about in phpMyAdmin, i read this line in the MySQL manual that explains why my query didn't work:[quote]Another restriction is that currently you cannot modify a table and select from the same table in a subquery. This applies to statements such as DELETE, INSERT, REPLACE, UPDATE, and (because subqueries can be used in the SET clause) LOAD DATA INFILE.[/quote]now i see why that makes sense, simply because the value you are SELECTing might change during or after the global UPDATE query. however, the update to take place depended on an aggregate (COUNT()) function from that same table. i've gotten around this for now by running them in separate queries, but i like to try to condense my queries where possible. anyone know of a way around this? my guess is no, but i'm not a MySQL guru. Quote Link to comment Share on other sites More sharing options...
fenway Posted October 11, 2006 Share Posted October 11, 2006 Usually one would use temporary tables. Quote Link to comment Share on other sites More sharing options...
akitchin Posted October 11, 2006 Author Share Posted October 11, 2006 in which case i guess an extra query is necessary regardless. alright, thanks. Quote Link to comment Share on other sites More sharing options...
fenway Posted October 11, 2006 Share Posted October 11, 2006 I'm not sure whether or not this is an MySQL limitation, or whether this is part of the SQL-2003 standard. If it's the former, they may have relaxed the restrictions in v5.x, but I haven't checked. Quote Link to comment Share on other sites More sharing options...
shoz Posted October 12, 2006 Share Posted October 12, 2006 You should be able to JOIN on a derived table which would hold the result of the subquery that I assume you're currently using in the WHERE clause.[code]UPDATEtable1 AS t1INNER JOIN( SELECT id FROM table1 GROUP BY id HAVING COUNT(*) > 2) AS t2ONt1.id = t2.idSETt1.column = ...[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.