子查询:
where型子查询 把内层的查询结果作为外层查询的比较条件。
from型子查询 把内层的查询结果作为临时表,供外层sql再次查询。
exists型子查询 把外层的查询结果,拿到内层,看内层的查询是否成立。
-------------------------------
where型子查询:
把内层的查询结果作为外层查询的比较条件。
1.查询出最新发布的商品信息
select goods_id,goods_name,shop_price from goods
where goods_id=(select max(goods_id) from goods);
2.查询出每个栏目最贵的商品
select goods_id,cat_id,goods_name,shop_price from goods
where shop_price in (select max(shop_pirce) from goods group by cat_id);
转载于:https://blog.51cto.com/1154179272/1653248