About Database Update (UPDATE statement) query __ Database

Source: Internet
Author: User
Tags one table table name

Talk about database update (UPDATE statement) queries

Today, someone in the group asked about the database updates, here, I will update the database of the problem to summarize
Frankly, the database update is a method update
Its standard format: Update table name Set field = value Where condition
But depending on the source of the data, there's a difference.

1. Input from external
This is relatively simple.
Example: Update TB set username= "XXXXX" where userid= "AASDD"

2. Some internal variables, functions, etc., such as time
Assign a function directly to a field
Update TB set Lastdate=date () where userid= "AASDD"

3. For some field variable +1, common such as: CTR, download number, etc.
This directly assigns the field +1 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 in one table to another table
Table1
ID F1 F2
Table2
ID F1 F2
Update F1 F2 in table2 to table1 (same ID) first

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 February product price in the table to January
Obviously, to find the same ID in the February and January e_id and update the price to January
This can be handled entirely with the above method, but due to the same table, in order to distinguish between 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, it is also possible to start the February query, in 5. The method 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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.