soni-b3196413
soni-b3196413

Reputation: 57

How to get a user with most cars from mysql

Here, is just a foo example.

    id user        car       
------ --------------------- 
     1 kerN         eTrac     
     2 rSky         TClea     
     3 ataw         arahm     
     4 nvd          lengr     
     5 233          yzxq2     
     6 kerN         eTrac     
     7 keOr         gStri     
     8 odev         eunic     
     9 owsm         cward     
    10 ielr         cgabr     
    11 kerN         eTrac  

From this table, I want to fetch the user with the most cars. In this case, the result should be the user KerN because they have 3 eTrac cars.

Upvotes: 0

Views: 39

Answers (1)

Dylan Su
Dylan Su

Reputation: 6065

Try this:

SELECT user, COUNT(*) cars 
FROM table 
GROUP BY user 
ORDER BY cars desc
LIMIT 1;

Upvotes: 2

Related Questions