MySQL Sub-query

Source: Internet
Author: User

1. Where sub-query

(The inner query result is treated as the comparison condition of the outer query )

#不用order by to find the latest products

Select  from where = (selectmax from goods);

#取出每个栏目下最新的产品 (goods_id only)

Select  from where inch (selectmaxfromgroup by
2, from type sub-query

(The inner query results for the outer layer again query )
#用子查询查出挂科两门及以上的同学的平均成绩
Ideas:
#先查出哪些同学挂科两门以上

Select name,count(*as fromwhere<  having >= 2;

#以上查询结果, we just have to name it, so we take a name again.

Select  from (Select name,count(*as from have>=2  as T;

#找出这些同学了, then calculate their average score.

Select name,avgfrom thewhere in (select name,count(*as from having>=2  as Group  by name;
3, Exists type sub-query

(Take the outer query results to the inner layer , see if the inner layer of the query is established)
#查询哪些栏目下有商品, column list category, Commodity table goods

Select  from where exists (Select*fromwhere= category.cat_id);

MySQL Sub-query

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.