Several update statement query methods

Source: Internet
Author: User

Zheng Wen:

Database update is a method of update,
Standard Format: Update table name set field = value where Condition
The data sources are different:


1. Input from outside
This is simpler than the limit.
Example: Update TB set username = "XXXXX" where userid = "aasdd"

2. Some internal variables and functions, such as time
Directly assign a function to a field
Update TB set lastdate = Date () Where userid = "aasdd"

3. For some field variables + 1, common examples include click-through rate and download times.
In this way, the field + 1 is directly assigned to itself.
Update TB set clickcount = clickcount + 1 where id = xxx

4. assign a 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.
Table1
Id F1 F2
Table 2
Id F1 F2
First, update F1 F2 in Table2 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 other records.
Table:
Id month e_id price
1 1 1 2
2 1 2 4
3 2 1 5
4 2 2 5
First, we need to update the product price in July to July.
Obviously, we need to find the same e_id as the ID in March and march, and update the price to mid March.
This can be completely processed with the above method, just because the same table, in order to distinguish two months, you should rename the table
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, we can also query the results in February and update them in the 5. method.

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

 

Several update statement query methods

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.