Obtenga los valores más comunes para todas las columnas

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

Aquí está la salida que quiero:

[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 todas las columnas?

SELECT COUNT(*) AS `Rows`, UserID
FROM table-name
GROUP BY UserID
ORDER BY `Rows` DESC

preguntado el 28 de agosto de 12 a las 14:08

I'm not clear enough, can you explain different? what do you expected to display? -

1 Respuestas

SELECT UserID, COUNT(*) FROM
(SELECT UserID FROM tablename
 UNION ALL
 SELECT UserID2 FROM tablename) t
GROUP BY UserID

No incluí el id column 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:

SELECT GROUP_CONCAT(id), UserID, COUNT(*) FROM
(SELECT id, UserID FROM tablename
 UNION ALL
 SELECT id, UserID2 FROM tablename) t
GROUP BY UserID

Respondido 29 ago 12, 13:08

Error: Every derived table must have its own alias - Floppy88

Thank you, now it works. I tried to add the id column too before COUNT but it returns an error. Where should I put it? - Floppy88

@WeContest, The values for the id column would be arbitrary. What do you want id for? - marcus adams

Is not possible to use original ids? - Floppy88

@WeContest, johnnietheblack shows up on 4 different records. Which id do you want to see? If you want to see them all, use GROUP_CONCAT(id), otherwise, you must specify which one you want. - marcus adams

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.