Home >Database >SQL >What is the sql command used to update records in the base table?

What is the sql command used to update records in the base table?

尚
Original
2019-07-24 15:35:469137browse

What is the sql command used to update records in the base table?

Database update is a method Update,
The standard format: Update table name set field = value where condition

However, there are differences depending on the source of the data:

1. Input from outside
This is relatively simple
Example:

update tb set UserName="XXXXX" where UserID="aasdd"

2. Some internal variables, Functions, etc., such as time, etc.
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, download times, etc.
This kind of directly assigns the field 1 Then assign it 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 table
table1
ID f1 f2
table2
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. Some records in one table are updated to other records
Table: a

##32154225
ID month E_ID Price
1 1 1 2
2 1 2 4
First update the product price in February to January in the table

Obviously, we need to find the E_ID with the same ID in February and January and update the price to January
This can be handled with the above method, but since it is the same table, in order to distinguish the 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 February first and then update it using method 5

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

Recommended: "

SQL Video Tutorial"

The above is the detailed content of What is the sql command used to update records in the base table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn