MySql 子查询

2017-03-02  本文已影响0人  zshanjun

使用子查询进行过滤

举例:

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\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\4.PNG

注意:上面给出的样例代码运行良好,但它并不是解决这种数据检索的最有效的方法


参考书籍:

上一篇 下一篇

猜你喜欢

热点阅读