sql >> Base de Datos >  >> RDS >> Mysql

mysql varios grupos por en una consulta

Puede combinar cada consulta agrupada con las demás usando UNION . Complete las columnas no utilizadas en cada subconsulta con NULL .

SELECT FirstName, LastName, NULL AS Country, NULL AS City, NULL AS BirthMonth, COUNT(*) AS count
FROM my_table
GROUP BY FirstName, LastName

UNION ALL

SELECT NULL, NULL, Country, City, NULL, COUNT(*)
FROM my_table
GROUP BY Country, City

UNION ALL

SELECT NULL, NULL, NULL, NULL, BirthMonth, COUNT(*)
FROM my_table
GROUP BY BirthMonth