Php sql avg使用导致结果集排序非法

Php sql avg使用导致结果集排序非法,php,mysql,Php,Mysql,这是我的SQL SELECT DISTINCT(scl_reviews.locid),avg(scl_reviews.rating) AS average,scl_locations.name,scl_locations.phone,scl_locations.address, scl_location.city,scl_locations.state,scl_locations.zip FROM scl_reviews LEFT JOIN scl_locations ON scl

这是我的SQL

 SELECT DISTINCT(scl_reviews.locid),avg(scl_reviews.rating)
 AS average,scl_locations.name,scl_locations.phone,scl_locations.address,
 scl_location.city,scl_locations.state,scl_locations.zip 
 FROM scl_reviews 
 LEFT JOIN scl_locations ON scl_locations.locid = scl_reviews.locid 
 GROUP BY scl_reviews.locid ORDER BY average DESC LIMIT 100
这意味着它将抓取评级最高的
位置
,并按平均值排序

问题是,平均值为5的记录查询位置的方式似乎有所不同有时我的id为3115的记录是位置1,有时是位置3。


不太清楚发生了什么,。我假设这与我的查询有关。

orderbyaverage DESC
未确定当存在相等值时的顺序。因此,只需按排序顺序列出一个唯一的列。您可能喜欢列号快捷方式:

ORDER BY 2 DESC, 1 DESC

ORDER BY average DESC
未确定当存在相等值时的顺序。因此,只需按排序顺序列出一个唯一的列。您可能喜欢列号快捷方式:

ORDER BY 2 DESC, 1 DESC

添加二级订单。当您的第一个订单具有相等的值时,这将生效。 检查新的order by子句:

SELECT DISTINCT(scl_reviews.locid),avg(scl_reviews.rating)
 AS average,scl_locations.name,scl_locations.phone,scl_locations.address,
 scl_location.city,scl_locations.state,scl_locations.zip 
 FROM scl_reviews 
 LEFT JOIN scl_locations ON scl_locations.locid = scl_reviews.locid 
 GROUP BY scl_reviews.locid 
 ORDER BY average DESC, scl_reviews.locid DESC LIMIT 100

添加二级订单。当您的第一个订单具有相等的值时,这将生效。 检查新的order by子句:

SELECT DISTINCT(scl_reviews.locid),avg(scl_reviews.rating)
 AS average,scl_locations.name,scl_locations.phone,scl_locations.address,
 scl_location.city,scl_locations.state,scl_locations.zip 
 FROM scl_reviews 
 LEFT JOIN scl_locations ON scl_locations.locid = scl_reviews.locid 
 GROUP BY scl_reviews.locid 
 ORDER BY average DESC, scl_reviews.locid DESC LIMIT 100

您的意思是在查询的不同运行中,平均数相等的几行的顺序不同吗?请尝试
按平均数排序DESC,scl_reviews.locid ASC
您的意思是在查询的不同运行中,平均数相等的几行的顺序不同吗?请尝试
按平均数排序DESC,scl_reviews.locid ASC