select cust_id from orders where order_num in (select order_num from orderitems where prod_id=’TNT2’);
在select语句中,子查询总是从内向外处理
作为计算字段使用子查询 假设需要显示customers表中每个客户的订单总数,订单与相应的客户ID存储在ordrs表中 为了执行这个操作,需要 1)从customers表中检索客户列表 2)对于检测出的每个客户,统计其在orders表中的订单数目 对客户10001的订单进行计数:select COUNT(*) AS orders from orders where cust_id=10001 为了对每个客户执行COUNT()计算,应该将COUNT()作为一个子查询 select cust_name,cust_state,(select COUNT(*) FROM orders where orders.cust_id= customers.cust_id) as orders from customers order by cust_name;
联结表
外键 外键为某个表中的一列,它包含另一个表的主键值,定义了两个表中的一个关系
创建联结 select vend_name, prod_name,prod_price from vendors,products where venders.vend_id = products.vend_id order by vend_name,prod_name;