简体   繁体   English

MySQL - 从表中选择唯一值

[英]MySQL - Select unique values from a table

My MySQL table contains the following information, 我的MySQL表包含以下信息,

ID  User            City            HomeLatitude      HomeLongitude
1   Egon Spengler   New York        19.123456         23.43546
2   Mac Taylor      New York        19.12343588       23.43546
3   Sarah Connor    New York        19.128376         34.35354
4   Jean-Luc Picard La Barre        11.345455         12.4356546
5   Ellen Ripley    Nostromo        32.76865          78.345435

From this table I need to query unique HomeLatitude values with first two decimal point because the first 3 location are almost same, so i need only the first row from the three rows.. 从这个表我需要查询前两个小数点的唯一HomeLatitude值,因为前三个位置几乎相同,所以我只需要三行中的第一行..

I used the below given query, 我用了下面给出的查询,

SELECT DISTINCT HomeLatitude, City FROM user;

But that will return all the rows from the table. 但是这将返回表中的所有行。 Can anyone help me on this. 谁可以帮我这个事。

ROUND函数将为您提供要显示的小数位数。

SELECT DISTINCT ROUND(HomeLatitude,2), City FROM user GROUP BY ROUND(HomeLatitude,2);

就像是:

   SELECT (DISTINCT ROUND(HomeLatitude, 1)) AS HomeLatitude, City FROM user;

While using a solution that is based on rounding a latitude or longitude may give you results, also be prepared for unexpected results. 使用基于舍入纬度或经度的解决方案可能会给您带来结果,同时也要为意外结果做好准备。 It is also possible for two places to have the exact same latitude but different longitudes and vice verce. 两个地方也可能具有完全相同的纬度但不同的经度和副情况。

If you are doing any kind of spatial analysis, you should invest in mysql spatial extensions and a function like ST_WITHIN 如果您正在进行任何类型的空间分析,您应该投资mysql空间扩展和像ST_WITHIN这样的函数

Second best option is to use the haversine formula or similar to find places that are close to each other. 第二个最佳选择是使用hasrsine公式或类似方法来查找彼此接近的位置。

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

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