Updating multiple rows
Notes: That index addition is necessary because on larger tables my SQL would rather die than figure to (internally) index a single column join.
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.
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).
There is a page in the online documentation that explains safe mode entitled 'safe Server Startup Script'.
Suppose you have a table where each row is associated with a certain group (For example, orders are associated with the customers placing them) where each item WITHIN the group has a distinct number (For example, each person my have a sequence of competition results - each person, therefore, has a 1st, 2nd, 3rd... If you would like to renumber items within their group so that each has the same baseline (say 0), here is an example way to proceed: Create TEMPORARY Table Groups (Id INTEGER AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(31), Group Id VARCHAR(31), Val Within Group INTEGER); INSERT INTO Groups VALUES (null, "Davy", "Boy", 2); INSERT INTO Groups VALUES (null, "Mary", "Girl", 2); INSERT INTO Groups VALUES (null, "Bill", "Boy", 5); INSERT INTO Groups VALUES (null, "Jill", "Girl", -3); INSERT INTO Groups VALUES (null, "Fred", "Boy", 3);# Find the lowest value for each group CREATE TEMPORARY TABLE Group Sum AS SELECT Group Id, MIN(Val Within Group) AS base Val FROM Groups GROUP BY Group Id;# create an index so my SQL can efficiently match ALTER TABLE Group Sum ADD UNIQUE (Group Id);# finally, make the baseline adjustment UPDATE Groups LEFT JOIN Group Sum USING (Group Id) SET Val Within Group=Val Within Group-base Val; SELECT * FROM Groups;# 1 Davy Boy 0# 2 Mary Girl 5# 3 Bill Boy 3# 4 Jill Girl 0# 5 Fred Boy 1#Each group ("Boy", "Girl") now has a (lowest) Val Within Group entry of 0.
when a row of a subset is lost/deleted/moved away from it, the whole subset will need to be reordered.
So, here it is again.]A very server resources friendly method to update multiple rows in the same table is by using WHEN THEN (with a very important note).
UPDATE tbl_name SET fld2 = CASE fld1WHEN val1 THEN data1WHEN val2 THEN data2ELSE fld2 ENDThe note is: do not forget ELSE.
For multiple-table updates, there is no guarantee that assignments are carried out in any particular order.
If you set a column to the value it currently has, My SQL notices this and does not update it.Here is a way to use multiple tables in your UPDATE statement, but actually copying one row values into the other, meaning, we're using the same table: UPDATE jobs AS to Table, jobs AS from Table SET to Table.job_type_id = from Table.job_type_id, to Table.job_company_id = from Table.job_company_id, to Table.job_source = from Table.job_source, WHERE (to Table.job_id = 6) AND (from Table.job_id = 1)--------------Pretty cool.