簡體   English   中英

使用GroupBy在MySQL中將具有不同隨機子查詢結果的Select子查詢隨機化

[英]Randomizing Select SubQuery in MySQL using GroupBy with different random SubQuery results

如何顯示同一國家/地區的不同members.name ,當前,如果顯示同一國家/地區的相同members.name ,但每次查詢執行時均顯示不同。

基於上一個問題: 使用GroupBy時在MySQL中隨機選擇子查詢

查詢:

 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

如上一個問題所示,您在country_streetsstreet_members之間沒有直接鏈接,因此我只看到以下解決方案:

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
        country_cities ON country_cities.country_id = country.id
            JOIN
        country_streets ON country_streets.city_id = country_cities.id
            JOIN
        (SELECT 
            RAND() as seed, country_id, city_id, cs.id as streets_id, guid, name
         FROM
            street_members sm 
            JOIN country_cities cc ON sm.country_id = cc.country_id 
            JOIN country_streets cs ON cs.city_id = cc.country_id 
         GROUP BY seed, name, guid,country_id,city_id,streets_id ORDER BY seed
        ) memebers ON memebers.streets_id=country_streets.id
    GROUP BY country.guid , country_cities.guid , country_streets.guid
    ORDER BY RAND()
    LIMIT 0 , 100

同樣,它沒有在您的特定數據庫上進行測試(並且肯定會在這里和那里得到改善),但是您明白了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM