开发者

MySQL - Find Client with most transactions on a table

开发者 https://www.devze.com 2023-01-12 06:12 出处:网络
I have开发者_C百科 a table of transactions that stores the transaction id, the client id and the total amount of each transaction.

I have开发者_C百科 a table of transactions that stores the transaction id, the client id and the total amount of each transaction. How do I find the client with more transactions? Im using PHP and Mysql, but im sure there is a way to do this inside the SQL query. Thanks.


There's lots of ways to do it, here's one

SELECT COUNT(client_id) AS transaction_count, client_id 
    FROM transactions
    GROUP BY client_id
    ORDER BY COUNT(client_id) DESC
    LIMIT 1


SELECT * FROM Transactions ORDER BY amount DESC LIMIT 1


One solution is:

SELECT `client_id`, COUNT(*) AS c_num 
FROM `transaction` 
GROUP BY `client_id` 
ORDER BY `c_num` ASC
LIMIT 1
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号