数据表格,创造数据都在上篇文章MySQL常用语句里,可以在我的栏目里看到。
这篇写MySQL使用子查询
对prod_id为RGAN01的所有订单物品,检索出order_num列,然后查询订单表中,订单号为prod_id为RGAN01的所有订单物品,检索出order_num列,查询顾客id
select cust_id from orders where order_num in(select order_num from orderitems where prod_id='RGAN01');
对prod_id为RGAN01的所有订单物品,检索出order_num列,然后查询订单表中,订单号为prod_id为RGAN01的所有订单物品,检索出order_num列,查询顾客id,最后根据查询到的顾客id查询顾客名字,和联系人
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='RGAN01'));