列表

详情


SQL91. 返回购买价格为 10 美元或以上产品的顾客列表

描述

OrderItems表示订单商品表,含有字段订单号:order_num、订单价格:item_price;Orders表代表订单信息表,含有顾客id:cust_id和订单号:order_num
OrderItems表
order_num item_price
a1 10
a2
1
a2
1
a4
2
a5
5
a2
1
a7 7
Orders表
order_num cust_id
a1 cust10
a2
cust1
a2
cust1
a4
cust2
a5
cust5
a2
cust1
a7 cust7
【问题】使用子查询,返回购买价格为 10 美元或以上产品的顾客列表,结果无需排序。
注意:你需要使用 OrderItems 表查找匹配的订单号(order_num),然后使用Order 表检索这些匹配订单的顾客 ID(cust_id)。
【示例结果】返回顾客id cust_id
cust_id
cust10
【示例解析】
cust10顾客下单的订单为a1,a1的售出价格大于等于10

示例1

输入:

DROP TABLE IF EXISTS `OrderItems`;
  CREATE TABLE IF NOT EXISTS `OrderItems`(
    order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
    item_price INT(16) NOT NULL COMMENT '售出价格'
  );
  INSERT `OrderItems` VALUES ('a1',10),('a2',1),('a2',1),('a4',2),('a5',5),('a2',1),('a7',7);

  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'
  );
  INSERT `Orders` VALUES ('a1','cust10'),('a2','cust1'),('a2','cust1'),('a4','cust2'),('a5','cust5'),('a2','cust1'),('a7','cust7');

输出:

cust10

原站题解

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

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

select f.cust_id from OrderItems t,Orders f
where t.order_num=f.order_num
and t.item_price>=10

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

SELECT DISTINCT cust_id
FROM (
    SELECT order_num
    FROM OrderItems
    WHERE item_price >= 10
) o JOIN Orders USING(order_num)

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

 select cust_id from Orders where order_num in (select order_num from OrderItems where item_price >=10);

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

select cust_id from OrderItems od,Orders os
where od.order_num=os.order_num
group by cust_id
having sum(item_price)>=10;

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

select cust_id from OrderItems a join Orders b on a.order_num=b.order_num  and item_price>=10