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