SQL93. 返回购买 prod_id 为 BR01 的产品的所有顾客的电子邮件(一)
描述
prod_id | order_num |
BR01 | a0001 |
BR01 | a0002 |
BR02 | a0003 |
BR02 | a0013 |
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 |
cust_id | cust_email |
cust10 | cust10@cust.com |
cust1 | cust1@cust.com |
cust2 | cust2@cust.com |
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