Leetcode586. 订单最多的客户(简单)

题目
查询 orders 表中下了最多订单的客户对应的 customer_number 。

题中只有一个客户的订单数比其他所有客户多。

orders 表描述如下:

Column  Type
order_number (PK)   int
customer_number int
order_date  date
required_date   date
shipped_date    date
status  char(15)
comment char(200)

输入样例

order_number    customer_number order_date  required_date   shipped_date    status  comment
1   1   2017-04-09  2017-04-13  2017-04-12  Closed  
2   2   2017-04-15  2017-04-20  2017-04-18  Closed  
3   3   2017-04-16  2017-04-25  2017-04-20  Closed  
4   3   2017-04-18  2017-04-28  2017-04-25  Closed  

输出样例

customer_number
3

解答
先选出每一个顾客id及其订单数目

select customer_number, count(*)
from orders AS O
group by customer_number

然后按订单数目进行排序, 取出第一个即可

select customer_number, count(*)
from orders 
group by customer_number
oreder by count(*) desc
limit 0, 1;

limit 起始条目索引, 显示的条目数
特点:
1、起始条目索引 默认0
2、limit 支持两个参数 1 显示的起始条目索引 2显示的条目数

语句1:select * from student limit 9,4
语句2:slect * from student limit 4 offset 9
// 语句1和2均返回表student的第10、11、12、13行

你可能感兴趣的:(Leetcode586. 订单最多的客户(简单))