1. 使用子查询
使用子查询
1) select order_num from orderitems where prod_id = 'TNT2';
2) select cust_id from orders where order_num in (20005, 20007);
SQL> select cust_id from orders where order_num in (select order_num from orderitems where prod_id = 'TNT2');
SQL> 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'));
作为计算字段使用子查询
SQL> select count(*) as orders from orders where cust_id = 10001;
SQL> select cust_name, cust_state, (select count(*) as orders from orders where orders.cust_id = customers.cust_id) as orders from customers order by cust_name;
1. 相关子查询 orders.cust_id = customers.cust_id
分享到:
2018-09-18 19:06
浏览 454
分类:数据库
评论