This article mainly takes you to understand and use theMySQL Update
statement to update existing records in the database table.
Basic syntax of the Update statement:
UPDATE <表名> SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ] [ORDER BY 子句] [LIMIT 子句]
SET
clause: used to specify the column name and column value to be modified in the table. Among them, each specified column value can be an expression or the default value corresponding to the column. If a default value is specified, the column value can be represented by the keyword DEFAULT.
WHERE
Clause: Optional. Used to limit the rows in the table to be modified. If not specified, all rows in the table will be modified.
ORDER BY
clause: optional. Used to limit the order in which rows in a table are modified.
LIMIT
clause: optional. Used to limit the number of rows that are modified.
First, create a new database:
CREATE TABLE tasks ( id INT NOT NULL, subject VARCHAR(45) NULL, start_date DATE NULL, end_date DATE NULL )charset utf8;
Secondly, insert data:
insert into tasks values(1,'math',2029-6-1,2060-6-1)
Finally, update the data:
UPDATE `tasks` SET `start_date`='2029-6-1', `end_date`='2060-6-1' WHERE (`id`='1') AND (`subject`='math') AND (`start_date`='0000-00-00') AND (`end_date`='0000-00-00') LIMIT 1
Note: Ensure thatUpdate
ends with aWHERE
clause, passed # The ##WHEREclause specifies the conditions that the updated records need to meet. If the
WHEREclause is ignored,
MySQLwill update all rows in the table.
The above is the detailed content of How to update table data in MySQL database. For more information, please follow other related articles on the PHP Chinese website!