Home > Database > Mysql Tutorial > What will MySQL return if the subquery used to assign a new value in the SET clause of an UPDATE statement returns multiple rows?

What will MySQL return if the subquery used to assign a new value in the SET clause of an UPDATE statement returns multiple rows?

王林
Release: 2023-08-27 19:13:02
forward
901 people have browsed it

如果用于在 UPDATE 语句的 SET 子句中分配新值的子查询返回多行,MySQL 将返回什么?

In this case, MySQL will return an error message because we know that if a subquery is used to assign a new value in the SET clause of the UPDATE statement, then it must be updated A WHERE clause is returned for each matching row in the table.

Example

mysql> insert into info(id, remarks) values(5,'average');
Query OK, 1 row affected (0.06 sec)

mysql> select * from info;
+------+-----------+
| id   | remarks   |
+------+-----------+
| 1    | Good      |
| 2    | Good      |
| 3    | Excellent |
| 4    | Average   |
| 5    | Best      |
| 5    | average   |
+------+-----------+
6 rows in set (0.00 sec)
Copy after login

As we have seen above, the "info" table has two rows with id = 5, so when we use it in a subquery, it will return multiple rows, Therefore MySQL will return an error message like this -< /p>

mysql> UPDATE STUDENT SET grade = (SELECT remarks from info WHERE info.id = student.id) WHERE id = 5;
ERROR 1242 (21000): Subquery returns more than 1 row
Copy after login

The above is the detailed content of What will MySQL return if the subquery used to assign a new value in the SET clause of an UPDATE statement returns multiple rows?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template