简体   繁体   English

无法理解在mysql查询中使用聚合函数的结果

[英]unable to understand the results of using aggregate function in mysql query

I have a table in my DB called cities, with id, name , country_id, lat(latitude) and lng(longitude). 我的数据库中有一个名为cities的表,其中包含id,name,country_id,lat(纬度)和lng(经度)。 In my application I get the user's location, then try to find out which city is closest to the user's location so at first I did something like this: 在我的应用程序中,我获取用户的位置,然后尝试找出哪个城市最接近用户的位置,所以一开始我做了这样的事情:

select * , ( sqrt( pow(lat - 31.205 , 2) + pow(lng - 29.907 , 2) ) ) as min_dis from cities

which returned results like this: 返回的结果如下:

+----+------------------+------------+---------------+---------------+----------------------+
| id | name             | country_id | lat           | lng           | min_dis              |
+----+------------------+------------+---------------+---------------+----------------------+
|  1 | Cairo            |         61 | 30.0444196000 | 31.2357116000 |   1.7642055948326205 |
|  2 | Alexandria       |         61 | 31.2000924000 | 29.9187387000 | 0.012723270627083274 |
|  3 | Tanta            |         61 | 30.7865086000 | 31.0003757000 |   1.1707286078440424 |
|  7 | North Coast      |         61 | 28.0488161000 | 34.4371483000 |    5.521208240105792 |
|  8 | Marsa Matruh     |         61 | 31.3543445000 | 27.2373159000 |    2.673858069059212 |
|  9 | Hurghada         |         61 | 27.2578957000 | 33.8116067000 |    5.552079415567052 |
| 10 | Ismailia         |         61 | 30.5964923000 | 32.2714587000 |   2.4415049795085366 |
| 11 | Ain ElSokhna     |         61 | 29.5927778000 | 32.3416667000 |    2.920079170546876 |
| 12 | El Mansoura      |         61 | 31.0409483000 | 31.3784704000 |    1.480587078948432 |
+----+------------------+------------+---------------+---------------+----------------------+

as you can see in the results "Alexandria" city, with ID=2 is the closest to the user's location. 正如您在结果“Alexandria”中看到的那样,ID = 2最接近用户的位置。

And when I try something like this: 当我尝试这样的事情时:

select MIN( sqrt( pow(lat - 31.205 , 2) + pow(lng - 29.907 , 2) ) ) as min_dis from cities;

I get this: 我明白了:

+----------------------+
| min_dis              |
+----------------------+
| 0.012723270627083274 |
+----------------------+

which makes sense to me... but when I try this: 这对我来说很有意义......但是当我尝试这个时:

select * , MIN( sqrt( pow(lat - 31.205 , 2) + pow(lng - 29.907 , 2) ) ) as min_dis from cities;

I get this: 我明白了:

+----+-------+------------+---------------+---------------+----------------------+
| id | name  | country_id | lat           | lng           | min_dis              |
+----+-------+------------+---------------+---------------+----------------------+
|  1 | Cairo |         61 | 30.0444196000 | 31.2357116000 | 0.012723270627083274 |
+----+-------+------------+---------------+---------------+----------------------+

the min_dis returned is correct, but the city data is not!! 返回的min_dis是正确的,但城市数据不是!! I thought it should have returned the record of "Alexandria" but it didn't .. Does anyone know why?? 我认为它本应该归还“亚历山大”的记录,但它没有..有谁知道为什么?

EDIT: this query worked for me: 编辑:这个查询对我有用:

SELECT `cities`.* FROM `cities` WHERE ((sqrt(pow(lat - 31.205 , 2) + pow(lng - 29.907 , 2)))= (SELECT MIN((sqrt(pow(lat - 31.205 , 2) + pow(lng - 29.907 , 2)))) AS min_id FROM `cities` ))

and returned: 并返回:

+----+------------+------------+---------------+---------------+
| id | name       | country_id | lat           | lng           |
+----+------------+------------+---------------+---------------+
|  2 | Alexandria |         61 | 31.2000924000 | 29.9187387000 |
+----+------------+------------+---------------+---------------+

All I just want is to understand why didn't this query work? 我只想要了解为什么这个查询不起作用?

select * , MIN( sqrt( pow(lat - 31.205 , 2) + pow(lng - 29.907 , 2) ) ) as min_dis from cities;

try this: 尝试这个:

select * , ( sqrt( pow(lat - 30.4990 , 2) + pow(lng - 29.4435 , 2) ) ) 
                                                    as min_dis 
from cities 
order by sqrt( pow(lat - 30.4990 , 2) + pow(lng - 29.4435 , 2) ) 
limit 1

You just have to do an order by in your first query to get the result 您只需在第一个查询中执行订单即可获得结果

When you use aggregate functions, any non-aggregate fields have to be correctly specified with the GROUP BY statement, otherwise you'll end up with an unspecified result, which is usually the first or last record used in the aggregate collation. 使用聚合函数时,必须使用GROUP BY语句正确指定任何非聚合字段,否则最终会得到未指定的结果,这通常是聚合排序规则中使用的第一个或最后一个记录。

MIN(...) does not mean return the minimum from the table, it means the minimum for the current group, which is specified by the leading fields and the GROUP BY statement. MIN(...)并不意味着从表中返回最小值,它表示当前组的最小值,由前导字段和GROUP BY语句指定。 If you want the minimum for the whole table, with the other records, you do need to either use an ORDER BY to get the lowest record, as Joe G Joseph did above, or do as you have done and tell it to match a subquery that gets the MIN(...) of the whole table via virtue of having no grouping fields. 如果你想要整个表的最小值和其他记录,你需要使用ORDER BY来获得最低记录,正如Joe G Joseph所做的那样,或者像你所做的那样做并告诉它匹配子查询通过没有分组字段获得整个表的MIN(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM