1. 使用子查询
使用子查询
1) select order_num from orderitems where prod_id = 'TNT2';
2) select cust_id from orders where order_num in (20005, 20007);
select cust_id from orders where order_num in (select order_num from orderitems where prod_id = 'TNT2');
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'));
作为计算字段使用子查询
select count(*) as orders from orders where cust_id = 10001;
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