Updating more than one column
If you do not use it, all rows that are outside the range of your updated values will be set to blank!If you wish to use an increment based on subset of a table you may combine UPDATE with Variables:e.g.There is a page in the online documentation that explains safe mode entitled 'safe Server Startup Script'.
I was not able, using my SQL 4.1.1, to do this as a subquery: UPDATE Groups LEFT JOIN (SELECT Group Id, MIN(Val Within Group) AS base Val FROM Groups GROUP BY Group Id) AS Grp Sum USING (Group Id) SET Val Within Group=Val Within Group-base Val; Csaba Gabor UPDATE Syntax with "on-line" updating value limitations.
Here is the closest I could come up with for doing that on Oracle:update t1 set t1.field=(select value from t2 where t1.this=t2.that) where t1in (select that from t2); That strikes me as convoluted by comparison.
order by recno desc limit 1You can also accomplish the same by the following query :update t1 , (select id ,max(recno) as recno from t1 where id=8 group by recno) ttset t1.c1 = ' NO'where tt.id=t1and t1.recno=tt.recno Comments are welcome.
Sometimes you have a lot of processes that could be updating a column value in a table. ID=54321 Here's a workaround for the update/subquery/cant do self table "bug"Senario is, ID 8 has multiple records, only the last (highest) record needs to be changedupdate t1 set c1 = ' NO'where id='8'order by recno desc limit 1I would prefer update t1 set c1=' NO' WHERE ID=8 AND RECNO = (SELECT MAX(RECNO) FROM T1 WHERE ID=8)But that's not currently allowed If you want to update a table based on an aggregate function applied to another table, you can use a correlated subquery, for example: UPDATE table1 SET table1field = (SELECT MAX(table2.table2field) FROM table2 WHERE table1.table1field = table2.table2field)This can be helpful if you need to create a temporary table storing an ID (for, say, a person) and a "last date" and already have another table storing all dates (for example, all dates of that person's orders).
If you want to return the value before you updated it without using a seperate select (which unless you lock the table could return a different value than is updated) then you can use a mysql variable like this:update some_table set col = col 1 where key = 'some_key_value' and @value := col The @value := col will always evaluate to true and will store the col value before the update in the @value variable. Additional information on My SQL correlated subqueries is at UPDATE can apparently be used to implement a semaphore (pardon my pseudocode):while TRUE The code above waits until the semaphore is "cleared" (value = 0) and then "sets" it (value = 1).