Discuss一覧用のリンク
https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/discuss/?currentPage=1&orderBy=most_votes&query=&tag=mysql
group by, order by, count, limit を使用した書き方
https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/discuss/1916054/MYSQL-oror-Database
解説も書いてあり嬉しい限りです。
ただこの方法だと注文数が同じユーザーがいる場合には対応していない。
Runtime: 786 ms, faster than 30.92% of MySQL online submissions for Customer Placing the Largest Number of Orders.
Memory Usage: 0B, less than 100.00% of MySQL online submissions for Customer Placing the Largest Number of Orders.
select customer_number from Orders group by customer_number order by count(customer_number) desc limit 1;
// Explanation:
// First we need to select customer_number from Orders
// Then we need to group by the same column i.e customer_number
// After that count the occurences of values in customer_number and then with the help of desc (descending) keyword I have converted it into descending order
// In the end I have used limit 1 so that only the max occurring element is shown in the output table
注文数が最も多いすべての顧客を返す方法
以下のリンクは3パターン用意されている。
Method3は複数ユーザーいた場合にも対応している。
https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/discuss/384220/3-Simple-MySQL-Solutions
実行速度も速い。
Runtime: 470 ms, faster than 70.32% of MySQL online submissions for Customer Placing the Largest Number of Orders.
Memory Usage: 0B, less than 100.00% of MySQL online submissions for Customer Placing the Largest Number of Orders.
select customer_number from orders
from Orders
group by customer_number
having count(order_number) = (select count(*) as count from Orders group by customer_number order by count desc limit 1)
ただし having count の使い方が自分のイメージと違い、わかっていない