Cannot located row updating
[I have posted this in the Flow Control Functions page last year but I still see people asking how to update multiple rows.
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).
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.
I had a problem - a had to update a column "rate" but if the existince or new value is greater then 5 this "5" will be finally value in field.
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.
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).You can use a database link to update a remote object only if you are using Oracle Database distributed functionality.If you omit to indicate that Oracle Database prohibits any changes to the table or view that would produce rows that are not included in the subquery.(Bug #11758262, Bug #50439) See Section 18.104.22.168, “Determination of Safe and Unsafe Statements in Binary Logging”, for more information.If you access a column from the table to be updated in an expression, assignments are generally evaluated from left to right.