1

嗨,我正在尝试获取按位置 ASC 排序的一些随机结果。

这是我的查询:

SELECT `location`, `route`
FROM (`foo`)
WHERE `location` != ''
ORDER BY  RAND(), `location` ASC
LIMIT 8 

问题是它随机获取但不按“位置”ASC排序,如果我这样做:

SELECT `location`, `route`
FROM (`foo`)
WHERE `location` != ''
ORDER BY  `location` ASC,RAND()
LIMIT 8 

它不是随机的。

我怎样才能同时获得RAND()ORDER BY location ASC

4

1 回答 1

3

您需要嵌套语句/查询:

SELECT *
FROM (
    SELECT `location`, `route`
    FROM `foo`
    WHERE `location` != ''
    ORDER BY RAND()
    LIMIT 8) AS `temp`
ORDER BY `location` ASC;
于 2013-01-14T08:45:32.803 回答