列表

详情


SQL90. 纠错3

描述

OrderItems表含有order_num订单号
order_num
a002
a002
a002
a004
a007
【问题】将下面代码修改正确后执行
SELECT order_num, COUNT(*) AS items 
FROM OrderItems 
GROUP BY items 
HAVING COUNT(*) >= 3 
ORDER BY items, order_num;
【示例结果】
返回订单号order_num和出现的次数items
order_num items
a002 3
【示例解析】
由于订单号a002出现了三次,所以返回3

示例1

输入:

DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	order_num VARCHAR(255) NOT NULL COMMENT '商品订单号'
);
INSERT `OrderItems` VALUES ('a002'),('a002'),('a002'),('a004'),('a007');

输出:

a002|3

原站题解

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

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

SELECT order_num, COUNT(order_num) AS items 
FROM OrderItems 
GROUP BY order_num 
HAVING items >= 3 
ORDER BY items, order_num;

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

SELECT order_num, COUNT(*) AS items 
FROM OrderItems 
GROUP BY order_num 
HAVING items >= 3 
ORDER BY items, order_num;

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

SELECT order_num, COUNT(*) AS items 
FROM OrderItems 
GROUP BY order_num
HAVING items >= 3 
ORDER BY items, order_num;

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

SELECT order_num,COUNT(*) AS items 
FROM OrderItems 
GROUP BY order_num 
HAVING COUNT(*) >= 3 
ORDER BY items, order_num;

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

select order_num, count(order_num) as items
from OrderItems
group by order_num
having items >= 3
order by items, order_num