如果 UPDATE 语句的 SET 子句中用于指定新值的子查询未返回任何行,MySQL 将返回什么?
在这种情况下,MySQL将向 SET 子句提供一个 NULL 值。以下示例将演示它−
示例
mysql> Select * from student; +----+---------+-----------+ | Id | Name | grade | +----+---------+-----------+ | 1 | Rahul | Good | | 2 | Gaurav | Good | | 3 | Raman | Excellent | | 4 | Harshit | Average | | 5 | Aarav | Best | | 6 | Ram | average | +----+---------+-----------+ 6 rows in set (0.00 sec) mysql> select * from info; +------+-----------+ | id | remarks | +------+-----------+ | 1 | Good | | 2 | Good | | 3 | Excellent | | 4 | Average | | 5 | Best | +------+-----------+ 5 rows in set (0.00 sec)
从上述两张表中,我们可以看到“学生”表中有6行,并且在 id = 6 的情况下,“成绩”的值为“平均”,而“信息”表中有5行。现在,在以下查询中使用的子查询将不会提供任何行来在 SET 子句中提供新值,因此它将插入一个 NULL 值。可以从以下查询的输出中检查它−
mysql> UPDATE STUDENT SET grade = (SELECT remarks from info WHERE info.id = student.id) WHERE id = 6; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> Select * from student; +----+---------+-----------+ | Id | Name | grade | +----+---------+-----------+ | 1 | Rahul | Good | | 2 | Gaurav | Good | | 3 | Raman | Excellent | | 4 | Harshit | Average | | 5 | Aarav | Best | | 6 | Ram | NULL | +----+---------+-----------+ 6 rows in set (0.00 sec)
广告