使用子查询进行过滤
举例:
select cust_name, cust_contact
from customers
where cust_id in (select cust_id
from orders
where order_num in (select order_num
from orderitems
where prod_id='TNT2'));
说明:列出订购物品TNT2的所有客户
\3.PNG作为计算字段使用子查询
select cust_name,
cust_state,
(select count(*)
from orders
where orders.cust_id = customers.cust_id) as orders
from customer
order by cust_name;
说明:显示customer表中每个客户的订单总数
\4.PNG注意:上面给出的样例代码运行良好,但它并不是解决这种数据检索的最有效的方法
参考书籍:
- MySQL必知必会
网友评论