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

Selección aleatoria de la subconsulta en MySQL cuando se usa GroupBy

Divertido, finalmente lo hice funcionar agregando las columnas que queríamos en la subconsulta en un group by , así:

SELECT 
    country.guid,
    country.Name AS 'country name',
    country.Area_id,
    country_cities.guid,
    country_cities.name AS 'city name',
    country_streets.guid,
    country_streets.name AS 'country streets',
    memebers.name.guid,
    memebers.name AS 'street members'
FROM
    country
        JOIN
    (SELECT 
        RAND() as seed, country_id, guid, name
    FROM
        street_members GROUP BY seed, name, guid,country_id ORDER BY seed) memebers ON memebers.country_id = country.id
        JOIN
    country_cities ON country_cities.country_id = country.id
        JOIN
    country_streets ON country_streets.city_id = country_cities.id
GROUP BY country.guid , country_cities.guid , country_streets.guid
ORDER BY RAND()
LIMIT 0 , 100

No probé toda la solicitud, pero probé el truco en una de mis bases de datos y funcionó.