列表

详情


SQL93. 返回购买 prod_id 为 BR01 的产品的所有顾客的电子邮件(一)

描述

你想知道订购 BR01 产品的日期,有表OrderItems代表订单商品信息表,prod_id为产品id;Orders表代表订单表有cust_id代表顾客id和订单日期order_date;Customers表含有cust_email 顾客邮件和cust_id顾客id
OrderItems表
prod_id order_num
BR01 a0001
BR01 a0002
BR02
a0003
BR02
a0013

Orders表
order_num cust_id order_date
a0001 cust10 2022-01-01 00:00:00
a0002 cust1 2022-01-01 00:01:00
a0003 cust1
2022-01-02 00:00:00
a0013 cust2
2022-01-01 00:20:00

Customers表代表顾客信息,cust_id为顾客id,cust_email为顾客email
cust_id cust_email
cust10 cust10@cust.com
cust1 cust1@cust.com
cust2 cust2@cust.com
【问题】返回购买 prod_id 为BR01 的产品的所有顾客的电子邮件(Customers 表中的 cust_email),结果无需排序。
提示:这涉及 SELECT 语句,最内层的从 OrderItems 表返回 order_num,中间的从 Customers 表返回 cust_id。

【示例结果】
返回顾客email cust_email
cust_email
cust10@cust.com
cust1@cust.com
【示例解析】 
产品id为BR01的订单a0001和a002的下单顾客cust10和cust1的顾客email cust_email分别是:cust10@cust.com 、cust1@cust.com

示例1

输入:

DROP TABLE IF EXISTS `OrderItems`;
  CREATE TABLE IF NOT EXISTS `OrderItems`(
    prod_id VARCHAR(255) NOT NULL COMMENT '产品id',
    order_num VARCHAR(255) NOT NULL COMMENT '商品订单号'
  );
  INSERT `OrderItems` VALUES ('BR01','a0001'),('BR01','a0002'),('BR02','a0003'),('BR02','a0013');

  DROP TABLE IF EXISTS `Orders`;
  CREATE TABLE IF NOT EXISTS `Orders`(
    order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
    cust_id VARCHAR(255) NOT NULL COMMENT '顾客id',
    order_date TIMESTAMP NOT NULL COMMENT '下单时间'
  );
  INSERT `Orders` VALUES ('a0001','cust10','2022-01-01 00:00:00'),('a0002','cust1','2022-01-01 00:01:00'),('a0003','cust1','2022-01-02 00:00:00'),('a0013','cust2','2022-01-01 00:20:00');

DROP TABLE IF EXISTS `Customers`;
CREATE TABLE IF NOT EXISTS `Customers`(
    cust_id VARCHAR(255) NOT NULL COMMENT '顾客id',
    cust_email VARCHAR(255) NOT NULL COMMENT '顾客email'
  );
INSERT `Customers` VALUES ('cust10','cust10@cust.com'),('cust1','cust1@cust.com'),('cust2','cust2@cust.com');

输出:

cust10@cust.com
cust1@cust.com

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Mysql 解法, 执行用时: 39ms, 内存消耗: 6448KB, 提交时间: 2022-03-03

select C.cust_email
from OrderItems O, Orders D, Customers C
where O.order_num = D.order_num
and D.cust_id = C.cust_id
and O.prod_id = 'BR01'

Mysql 解法, 执行用时: 39ms, 内存消耗: 6460KB, 提交时间: 2022-03-03

select cust_email
from OrderItems as oi join Orders as o join Customers as c
on oi.order_num = o.order_num and o.cust_id = c.cust_id
where prod_id='BR01';

Mysql 解法, 执行用时: 39ms, 内存消耗: 6500KB, 提交时间: 2022-03-05

SELECT cust_email
FROM Customers
WHERE cust_id IN (
    SELECT cust_id
    FROM Orders
    WHERE order_num IN (
        SELECT order_num
        FROM OrderItems
        WHERE prod_id='BR01'
    )
)

Mysql 解法, 执行用时: 39ms, 内存消耗: 6508KB, 提交时间: 2022-03-07

select cust_email from Customers where cust_id in
(select cust_id from
OrderItems aa join Orders bb on aa.order_num=bb.order_num
where aa.prod_id='BR01')

Mysql 解法, 执行用时: 40ms, 内存消耗: 6456KB, 提交时间: 2022-03-07

select t3.cust_email 
from OrderItems t1,Orders t2,Customers t3
where t1.prod_id='BR01'
and t1.order_num=t2.order_num
and t2.cust_id=t3.cust_id