如果值在 MySQL 中相同,UPDATE 是否会覆盖值
mysqlmysqli database
不会,如果值相同,MySQL UPDATE 不会覆盖值。让我们先创建一个表 −
mysql> create table DemoTable ( StudentId int, StudentMathMarks int, StudentMySQLMarks int ); Query OK, 0 rows affected (0.46 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(1,56,78); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(2,88,99); Query OK, 1 row affected (0.15 sec) mysql> inse rt into DemoTable values(3,34,98); Query OK, 1 row affected (0.13 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-----------+------------------+-------------------+ | StudentId | StudentMathMarks | StudentMySQLMarks | +-----------+------------------+-------------------+ | 1 | 56 | 78 | | 2 | 88 | 99 | | 3 | 34 | 98 | +-----------+------------------+-------------------+ 3 rows in set (0.00 sec)
以下是更新值的查询。该特定列中已存在设置的值,即学生 ID 2、数学分数 88 和 SQL 分数 99(学生 ID 2) −
mysql> update DemoTable set StudentId=2,StudentMathMarks=88,StudentMySQLMarks=99 where StudentId=2; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0
查看上面的 UPDATE 命令,输出 0 rows impacted 表示如果值相同,UPDATE 不会覆盖它们。