mysql学习(六)表字段值的修改

修改值

基本语法
update **table_name** set **col_name1**=expr1,[col_name2=expr1] where where_condition
实例
修改分数低于60的人的分数到59


MariaDB [istester]> select * from idoxu where grade < 60;
+----+--------+----------+----------+-------+
| id | stu_id | c_name   | istester | grade |
+----+--------+----------+----------+-------+
|  6 |     33 | istester | NULL     |    20 |
+----+--------+----------+----------+-------+
1 row in set (0.00 sec)

MariaDB [istester]> select * from idoxu;
+----+--------+----------+----------+-------+
| id | stu_id | c_name   | istester | grade |
+----+--------+----------+----------+-------+
|  4 |     11 | idoxu    | NULL     |    90 |
|  5 |     12 | lin      | NULL     |   100 |
|  6 |     33 | istester | NULL     |    20 |
+----+--------+----------+----------+-------+
3 rows in set (0.00 sec)

MariaDB [istester]> update idoxu set grade=59 where grade < 60;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [istester]> select * from idoxu;
+----+--------+----------+----------+-------+
| id | stu_id | c_name   | istester | grade |
+----+--------+----------+----------+-------+
|  4 |     11 | idoxu    | NULL     |    90 |
|  5 |     12 | lin      | NULL     |   100 |
|  6 |     33 | istester | NULL     |    59 |
+----+--------+----------+----------+-------+
3 rows in set (0.00 sec)

修改性别值为空的数据为0

MariaDB [istester]> select * from istester where sex is NULL;
+----+--------+------+-------+------------+---------+-------+
| id | uname  | sex  | birth | department | address | idoxu |
+----+--------+------+-------+------------+---------+-------+
|  1 | idoxu  | NULL |  NULL | NULL       | NULL    | 2020  |
|  2 | idoxu2 | NULL |  NULL | NULL       | NULL    | 2020  |
+----+--------+------+-------+------------+---------+-------+
2 rows in set (0.00 sec)
MariaDB [istester]> update istester set sex=0 where sex is null;
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2  Changed: 2  Warnings: 0

MariaDB [istester]> select * from istester;
+----+--------+------+-------+------------+---------+-------+
| id | uname  | sex  | birth | department | address | idoxu |
+----+--------+------+-------+------------+---------+-------+
|  1 | idoxu  | 0    |  NULL | NULL       | NULL    | 2020  |
|  2 | idoxu2 | 0    |  NULL | NULL       | NULL    | 2020  |
| 11 | idoxu3 | 1    |  NULL | NULL       | NULL    | NULL  |
| 12 | idoxu4 | 2    |  NULL | NULL       | NULL    | NULL  |
| 13 | idoxu5 | 1    |  NULL | NULL       | NULL    | NULL  |
| 14 | idoxu6 | 2    |  NULL | NULL       | NULL    | NULL  |
+----+--------+------+-------+------------+---------+-------+
6 rows in set (0.00 sec)

注意:mysql中值的判断有=,<,>,<=,>=,但是针对空值的情况下不能用=NULL去判断,NULL专门有个关键字is去判断

你可能感兴趣的:(mysql学习(六)表字段值的修改)