使用具有不同随机SubQuery结果的GroupBy随机选择MySQL中的SubQuery

时间:2018-06-18 15:28:33

标签: mysql

如何为同一个国家/地区显示不同的members.name,目前如果同一个国家/地区显示相同的members.name,但每次查询执行时显示不同的 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

基于上一个问题:Randomizing Select subquery in MySQL when using GroupBy

查询:

while True:
    ret, frame = cap.read() # gets None and False
    cv2.imshow('VIDEO', frame)
    k = cv2.waitKey(0) & 0xFF
    if k == 27: #esc key ends process
        cap.release()
        break
    #print(ret)#Prints Noce
    #print(frame)#Print False
    cv2.destroyAllWindows()
    cv2.waitKey(1)

1 个答案:

答案 0 :(得分:0)

如上一个问题所示,您在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

同样,它没有在您的特定数据库上进行测试(并且肯定会在这里和那里得到改进),但您明白了。