|brotherhood of LAN|
| 5:05 pm on Oct 19, 2010 (gmt 0)|
Did you try it and it worked migthegreek?
Check out subqueries [dev.mysql.com] for more info.
| 9:08 am on Oct 20, 2010 (gmt 0)|
Yes, and it doesn't work.
"You can't specify target table 'temp_users' for update in FROM clause "
| 10:14 am on Oct 20, 2010 (gmt 0)|
UPDATE temp_users SET notes = (SELECT a.notes FROM temp_users a WHERE id = 1) WHERE id = 2
| 10:45 am on Oct 20, 2010 (gmt 0)|
Still get MySQL error:
#1093 - You can't specify target table 'temp_users' for update in FROM clause
I think I already tried using table aliases.
| 10:59 am on Oct 20, 2010 (gmt 0)|
in which case try something like this.
DECLARE myvariable VARCHAR(100); -- whatever the notes field is
SET myvariable = (SELECT notes FROM temp_users WHERE id = 1);
UPDATE temp_users SET notes = myvariable WHERE id = 2;
is this a one off thing? or something you'll need to do often?
| 11:14 am on Oct 20, 2010 (gmt 0)|
Something I will need to do often. But I still get an error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE myvariable VARCHAR(100)' at line 1
| 11:37 am on Oct 20, 2010 (gmt 0)|
did you delete the comment?
the sequence i suggested should work, maybe you need to make it all a stored procedure? and then call the stored procedure from your php instead of sending a select statement.
i admit i only use stored procedures for all kinds of reasons, so i'm unsure of the syntax to send multiple queries to mySQL from php
are you doing this from a php page or from another tool directly interacting with the database?
alternatively the obvious workaround is within the php code, query the database to get the required notes then query the database again to update the appropriate field.
| 11:44 am on Oct 20, 2010 (gmt 0)|
Yes, sorry - I will explain a bit more. This query needs to be run in phpMyAdmin, although I have not used variables before. It is for a colleague of mine, to run this query occasionally but if all else fails I will simply build him a PHP page. I was hoping there is a quick way using a direct SQL statement.
| 4:33 pm on Oct 20, 2010 (gmt 0)|
|Currently, you cannot update a table and select from the same table in a subquery. |
From the manual [dev.mysql.com]
| 9:41 pm on Oct 21, 2010 (gmt 0)|
are you trying to do this for the whole table? eg possibly 100's or 1000's of rows at teh same time?
or are you trying to do it for one or two KNOWN rows ( eg you know the id or id's) that you will want to update?
| 10:09 am on Oct 22, 2010 (gmt 0)|
Use couple of aliases for the same table you don't need a select.
UPDATE temp_users tu, temp_users ts SET tu.notes = ts.notes where tu.id=1 and ts.id=2
| 11:49 am on Oct 22, 2010 (gmt 0)|
excellent solution enigma1, i didn't think of that at all
| 9:13 am on Oct 25, 2010 (gmt 0)|
Works perfectly, thank you. Never would have thought of that myself.