There are three sub-queries: Where sub-query, from sub-query, exist subquery.
where sub-queryWhere subquery: refers to the query result of the inner layer as the comparison condition of the outer query. Example: Check out the latest product: Select Good_id,good_name from good where good_id = (select Max (good_id) from good);--this sentence is a subquery Select Good_id,good_name from good order by good_id DESC limit 1;
From sub-query
From subquery: The query results of the inner layer are treated as temporary tables for the outer SQL to query again. Example: Query the latest item under each column: Select Good_id,cat_id,good_name from Good where good_id in (select Max (good_id) from good Group B y cat_id);---Typical where subquery select * FROM (select Good_id,cat_id,good_name from Good order by cat_id asc,good_id de SC) as TMP GROUP BY Cat_id;--from Sub-query
exists sub-queryExists sub-query: The outer query results, get the inner layer, look at the inner layer of the query is set up. Typical example: Select Cat_id,cat_name from category where exists (select * from good where good.cat_id = category.cat_id), and this subquery is now less used , has been replaced by join. Select Cat_id,cat_name from category join good on category.cat_id = good.cat_id;
Basic SQL Statement--sub-query