Basic syntax of UPDATE
UPDATE
Statements are used to modify data in an existing table. It usually hasSET
Use together to specify the field to update and its new value. You can also useWHERE
clause to limit records to be updated.
Basic syntax:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
-
table_name
: The name of the table to be updated. -
column1
,column2
: The field name to be updated. -
value1
,value2
: The updated new value. -
condition
: Conditions that limit the update scope. If notWHERE
clause, updates all records in the table.
Update single record
To update a single record, you need to useWHERE
clause to ensure that only records that meet the criteria are updated.
Example: Update the email address of the user with username ‘alice’
UPDATE users SET email = '@' WHERE username = 'alice';
This statement willusers
In the tableusername
For users of ‘alice’email
Updated to ‘@’.
Update multiple records
You can update multiple records with the right conditions.
Example: Allstatus
For users of ‘inactive’status
Updated to ‘active’
UPDATE users SET status = 'active' WHERE status = 'inactive';
This statement willusers
All in the tablestatus
The record for ‘inactive’ is updated to ‘active’.
Restrict update records using WHERE
To avoid updating all records, you can useWHERE
clauses to limit the update scope. NoWHERE
When a clause, all records in the table are updated.
Example: Updated all users older than 30 have the status of ‘senior’
UPDATE users SET status = 'senior' WHERE age > 30;
This statement is updated onlyage
Users greater than 30.
Update multiple fields using SET
You can be in the sameUPDATE
To update multiple fields in a statement, just use commas to separate the assignments of each field.
Example: Update the user'semail
andstatus
UPDATE users SET email = '@', status = 'active' WHERE username = 'bob';
This statement willusername
For users of ‘bob’email
Updated to ‘@‘, andstatus
Updated to ‘active’.
Update with subqueries
existUPDATE
In a statement, subqueries can be used to calculate updated values dynamically.
Example: Update the order status in the orders table to ‘shipped’ and change itshipped_date
Set as current date
UPDATE orders SET order_status = 'shipped', shipped_date = (SELECT CURRENT_DATE()) WHERE order_status = 'processing';
This statement willorder_status
The status of all orders for ‘processing’ is updated to ‘shipped’ and theshipped_date
Set to the current date.
UPDATE Use JOIN
You can also useJOIN
clause to update the data in the table.
Typically, this is used to update records based on the values of another table.
Example: Update the user_email field in the orders table based on the email in the users table
UPDATE orders o JOIN users u ON o.user_id = SET o.user_email = WHERE o.order_status = 'pending';
This statement willorders
All in the tableorder_status
Recorded for ‘pending’user_email
Updated to correspondingusers
The tableemail
。
Use LIMIT to limit the number of updates
By usingLIMIT
clause, you can limit the number of updated items.
In some cases, you may want to update only the first few records in the table.
Example: Update status of the first 5 records in the users table
UPDATE users SET status = 'inactive' LIMIT 5;
This statement willusers
The first 5 records in the tablestatus
Updated to ‘inactive’.
References:
- MySQL UPDATE official documentation
- MySQL JOIN official documentation
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.