I want to get the most common values for all columns in a mySQL table.
[id] [UserID] [UserID2]
1 johnnietheblack marywhite
2 johnnietheblack marywhite
3 dannyrottenegg dannyrottenegg
4 marywhite marywhite
5 marywhite johnnietheblack
6 johnnietheblack marywhite
Here’s the output I want:
[id] [UserID] [Count]
1 johnnietheblack 4
2 dannyrottenegg 2
3 marywhite 6
I can use the following to get the common values for one columns. But how can I get the common values for all columns?
SELECT COUNT(*) AS `Rows`, UserID
FROM table-name
GROUP BY UserID
ORDER BY `Rows` DESC
I didn’t include the
idcolumn because it doesn’t seem to correlate with the values.If you want to see all of the ids for each name, you can use GROUP_CONCAT: