gpt4 book ai didi

mysql - 使用具有不同随机子查询结果的 GroupBy 随机化 MySQL 中的 Select SubQuery

转载 作者:行者123 更新时间:2023-11-29 17:26:43 24 4
gpt4 key购买 nike

如何为同一国家/地区显示不同的 members.name,当前是否为同一国家/地区显示相同的 members.name,但每次查询执行时显示不同的。

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

查询:

 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

同样,它没有在您的特定数据库上进行测试(并且肯定可以到处改进),但您明白了。

关于mysql - 使用具有不同随机子查询结果的 GroupBy 随机化 MySQL 中的 Select SubQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50912857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com