Oracle several update (Update statement) query methods

Database update is a method Update,
its standard format: Update table name set field = value where condition
However, according to the source of the data, it is still different:

 

1. Input this relatively simple example from the outside
: update tb set UserName="XXXXX" where UserID="aasdd"

2. Some internal variables, functions, etc., such as time,
directly assign the function to the field
update tb set LastDate=date( ) where UserID="aasdd"

3. For some field variables +1, common ones such as: click rate, number of downloads, etc.
directly add 1 to the field and then assign it to itself
update tb set clickcount=clickcount+1 where ID=xxx

4. Assign one field of the same record to another field update tb set Lastdate
= regdate where XXX

5. Update a batch of records from one table to another table
table1 
ID f1 f2
table2 
ID f1 f2
f1 f2 update to table1 (same ID)

update table1,table2 set table1.f1=table2.f1,table1.f2=table2.f2 where table1.ID=table2.ID

6. Update some records in the same table to some other records
table : a
ID month E_ID Price
1 1 1 2
2 1 2 4
3 2 1 5
4 2 2 5
First update the product price in February to January in the table.
Obviously, you need to find the E_ID with the same ID in February and January and update the price to In January,
this can be handled with the above method, but because of the same table, in order to distinguish two months, the table should be renamed
update a, a as b set a.price=b.price where a.E_ID= b.E_ID and a.month=1 and b.month=2  Of course, you can also query for

February first , and use the method of 5. to update  update a, (select * from a where month=2) as b set a.price=b.price where a.E_ID=b.E_ID and a.month=1


 

 

 

 

 

 

Multi-table association UPDATE

Suppose there are two tables A and B, A table fields a, b, c, d, B table fields b, e, f, the association condition of the two tables is field b, and now I want to make a data patch, I want to add the B table The value of field e is patched to field c of table A.

There are two methods as follows:

update A set A.c=(select e from B where B.b=A.b)
where exists(select 1 from B where B.b=A.b);

 

merge into A
using B
on (A.b=B.b)
when matched then update set A.c=B.e;

 The above two methods can realize the update of multi-table join, and the B table can also be a subquery or view.

Merge into is a syntax added after oracle 9i, which can realize the function of update/insert (conditional update, not conditional insert), and it is more efficient, because only one full table scan is required for merge, but the use of merge into needs to be careful , you must understand its usage before you can use it with confidence, otherwise there may be problems.

 

The above example can update not only a single field, but also multiple fields, as follows:

update A set A.c=(select e from B where B.b=A.b),
             A.d=(select f from B where B.b=A.b)
where exists(select 1 from B where B.b=A.b);



merge into A
using B
on (A.b=B.b)
when matched then update set A.c=B.e,
                             A.d=B.f;

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326366430&siteId=291194637