Sub-query in "MYSQL" update/delete/select statement

Source: Internet
Author: User

When an UPDATE or DELETE statement contains a subquery, the table in the subquery cannot be in the update or DELETE statement, as if it contains runtime error, but when the SELECT statement contains a subquery, the table in the subquery can be in the SELECT statement.

For example, change the name of a student with a total of less than 100 to a genius

Select stu_id from Score GROUP by stu_id have sum (grade) <100; #查询总成绩小于100的学生ID
Update students set Name= ' genius ' where ID in (select stu_id from Score GROUP by stu_id have sum (grade) <100);

In addition, when you do the query, you can use a single table do not use multiple tables. In this way, the logic is clear, the probability of error is much less.

Sub-query in "MYSQL" update/delete/select statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.