UPDATE tbl_name SET fld2 = CASE fld1WHEN val1 THEN data1WHEN val2 THEN data2ELSE fld2 ENDThe note is: do not forget ELSE.
If you do not use it, all rows that are outside the range of your updated values will be set to blank!
If you omit the WHERE clause, all records in the table will be updated!
The WHERE clause specifies which record(s) that should be updated.
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.
Ideal when writing a script is just too much effort.
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).
His example was:update t1,t2 set t1.field=t2.value where t1.this=t2.that; That strikes me as an elegant syntax.
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.