1、作用:
对某个单元格中的值进行更改。
2、命令格式
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
(UPDATE t_user SET name='ABC' WHERE id=2;)
你可以同时更新一个或多个字段。
你可以在 WHERE 子句中指定任何条件。
你可以在一个单独表中同时更新数据。
3、例子
3.1
mysql> select * from t_user;
+------+------+
| id | name |
+------+------+
| 1 | Tom |
| 2 | Jack |
+------+------+
2 rows in set (0.00 sec)
mysql> UPDATE t_user SET name='ABC' WHERE id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t_user;
+------+------+
| id | name |
+------+------+
| 1 | Tom |
| 2 | ABC |
+------+------+
2 rows in set (0.00 sec)
mysql>
3.2 更改时可以往一个单元格中写入多个值,以逗号或者空格间隔都可以
mysql> select * from t_user;
+------+------+
| id | name |
+------+------+
| 1 | Tom |
| 2 | ABC |
+------+------+
2 rows in set (0.00 sec)
mysql> UPDATE t_user SET name='ABC Mary' WHERE id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t_user;
+------+----------+
| id | name |
+------+----------+
| 1 | Tom |
| 2 | ABC Mary |
+------+----------+
2 rows in set (0.00 sec)
mysql> UPDATE t_user SET name='BCD,EFG Mary' WHERE id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t_user;
+------+--------------+
| id | name |
+------+--------------+
| 1 | Tom |
| 2 | BCD,EFG Mary |
+------+--------------+
2 rows in set (0.00 sec)
mysql>
网友评论