JoaoPedro
JoaoPedro

Reputation: 511

MySQL - Find Client with most transactions on a table

I have 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.

Upvotes: 1

Views: 230

Answers (3)

assaqqaf
assaqqaf

Reputation: 1585

One solution is:

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

Upvotes: 0

Alex Pliutau
Alex Pliutau

Reputation: 21947

SELECT * FROM Transactions ORDER BY amount DESC LIMIT 1

Upvotes: 0

ircmaxell
ircmaxell

Reputation: 165271

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

Upvotes: 1

Related Questions