p2868156986 2022-09-30 16:34 采纳率: 75%

# mysql的临时表怎么用呢？以leetcode题《订单最多的客户》为例

1、先按customer_number分组，用count()函数求出customer_number的订单总数

``````select customer_number, count(*) as coun from Orders group by customer_number

``````

2、然后在该基础上使用max()函数得到订单总数的最大值，进而得出结果

#### 我的答案如下：

``````select
customer_number
from
(select customer_number, count(*) as coun from Orders group by customer_number) as t1
where
coun=
(select max(coun) from
(select customer_number, count(*) as coun from Orders group by customer_number) as t2)

``````

``````select
customer_number
from
(select customer_number, count(*) as coun from Orders group by customer_number) as t1
where coun=(select max(coun) from t1)

``````

``````DROP TEMPORARY TABLE IF EXISTS t1;
CREATE TEMPORARY TABLE t1 AS
(select customer_number, count(*) as coun from Orders group by customer_number);

``````

``````DROP TEMPORARY TABLE IF EXISTS t1;
CREATE TEMPORARY TABLE t1 AS
(select customer_number, count(*) as coun from Orders group by customer_number);
select
customer_number
from
t1
where coun=(select max(coun) from t1);

``````

``````select
customer_number
from
t1
where coun=(select max(coun) from t1);

``````

#### 所以问题是：

``````select
customer_number
from
(select customer_number, count(*) as coun from Orders group by customer_number) as t1
where coun=(select max(coun) from t1)

``````

• 写回答

#### 2条回答默认 最新

• 心寒丶 全栈领域优质创作者 2022-09-30 16:59
关注

直接这样不行么，为什么还用子查询呢
select customer_number, count(*) as coun from Orders group by customer_number ORDER BY count desc LIMIT 1 ;
子查询只限定于可以查询子表中拥有的字段，但是不能再把子表当作表去查询

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论 编辑记录

• 系统已结题 10月15日
• 已采纳回答 10月7日
• 创建了问题 9月30日

#### 悬赏问题

• ¥15 思科WS-C4503-E核心交机
• ¥15 批量提取WORD信息到EXCEL
• ¥15 git后台提示“指定的网络不可用”如何处理？
• ¥15 Omi框架如何兼容Vue
• ¥15 windows api中有没有可以通过scsi设备发送离散buffer的接口，如何解决？（相关搜索：linux系统|结构体）
• ¥15 r语言练习题，三个问题都有疑惑
• ¥15 绝对路径都行，为什么相对路径不行
• ¥20 ARM64架构下SSCMS部署问题
• ¥20 请教如何让移动端的热区自适应
• ¥30 为什么深度学习网络训练会出现训练集和测试集准确率和损失都不变的情况