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