
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What MySQL Returns for Sub-query in UPDATE Statement
In this case, MySQL will return an error message because we know that if sub-query is used to assign new values in the SET clause of UPDATE statement then it must return exactly one row for each row in the update table that matches the WHERE clause.
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)
As we can see above ‘info’ table is having two rows with id = 5 hence when we will use it in the sub-query it will return multiple rows and due to this MySQL will return an error message as follows −
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
Advertisements