SQL91. 返回购买价格为 10 美元或以上产品的顾客列表
描述
order_num | item_price |
a1 | 10 |
a2 | 1 |
a2 | 1 |
a4 | 2 |
a5 | 5 |
a2 | 1 |
a7 | 7 |
order_num | cust_id |
a1 | cust10 |
a2 | cust1 |
a2 | cust1 |
a4 | cust2 |
a5 | cust5 |
a2 | cust1 |
a7 | cust7 |
cust_id |
cust10 |
示例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