介绍了mysql子查询,所有的查询结果均可当作表使用,子查询按照使用位置可以分为:作为查询参数、作为表等方式使用
所有的查询结果都是一张表,我们可以在查询的结果基础上做任何关于表的操作
子查询可以作为表使用,也可以作为查询条件使用
-- 查询ANV01商品的顾客
select
cust_name
,cust_address
,cust_city
,cust_state
,cust_country
from customers
where cust_id in(
-- 查询买过ANV01订单的顾客
select cust_id
from orders
where order_num in (
-- 查询买过ANV01的订单
select order_num
from orderitems
where prod_id = 'ANV01'
)
)
;
-- 查询销量最多的商品名称
select prod_name
from products
where prod_id in(
select
prod_id
from orderitems
group by prod_id
having sum(quantity) = (
select max(quan)
from(
select
prod_id,sum(quantity) as quan
from orderitems
group by prod_id
) t -- t 是代指 子查询结果表的表名。子查询作为表使用时需要指定别名,但是作为筛选条件使用时不需要指定别名
)
);