如果子查询(用于在UPDATE语句的SET子句中分配新值)不返回任何行,则MySQL返回什么?
在这种情况下,MySQL将为SET子句提供NULL值。以下示例将演示它-
示例
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) 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值。可以从以下查询的输出中检查它-
UPDATE STUDENT SET grade = (SELECT remarks from info WHERE info.id = student.id) WHERE id = 6; Rows matched: 1 Changed: 1 Warnings: 0 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)