Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL, SELECT * FROM t WHERE c={The most duplicated entry}

Tags:

mysql

Pretty much as the title says, that was the simplest way I could explain it. To elaborate...

I first need to find the value of column c that has been duplicated the most times (mostDuplicated), and then SELECT * FROM t WHERE c=mostDuplicated

To go on about it further...

Here's my data:

SELECT * FROM t

a, b, c
-  -  -
1, 1, 1
2, 2, 1
3, 3, 1
4, 4, 2
5, 5, 3

So ignore the values in columns a & b completely, just concentrate on column c. I need to find the most duplicated value in column c (which is 1), and then SELECT only these records WHERE c=1. I want to do this in a single query if possible.

like image 577
Drahcir Avatar asked Dec 22 '22 04:12

Drahcir


2 Answers

Do a "group by" query to count the number of unique values of c, order it descending and select only the top row. Then use the output as a subquery to select rows with that particular value of c:

SELECT * FROM t WHERE c = (SELECT c FROM t GROUP BY c ORDER BY COUNT(*) DESC LIMIT 1)
like image 102
bobbymcr Avatar answered Jan 12 '23 21:01

bobbymcr


SELECT c FROM t GROUP BY c ORDER BY count(*) DESC LIMIT 1

like image 32
Kenaniah Avatar answered Jan 12 '23 22:01

Kenaniah