简体   繁体   English

我如何在mysql中根据最近的经度和纬度点将表A中的所有行与表b中的所有行进行匹配?

[英]How do i in mysql match all rows in table A with all rows in table b based on the nearest longitude and latitude point?

HI Guys first question here. 大家好,第一个问题在这里。 I have simplified the table structure below to make it more apparent the problem. 我简化了下面的表结构,使问题更加明显。 I am using mysql and I have no idea where to start with this one. 我正在使用mysql,但我不知道从哪里开始。 I need to find the closest store for each of the customers in my database. 我需要在数据库中为每个客户找到最近的商店。

I have table "A" which contains the following 我有表“ A” ,其中包含以下内容

id primary key
customer - name of my customer
longitude
latitude

I have table "B" 我有桌子“ B”

id 
storeName - name of one of our branches
longitude
latitude
storeAddress - Address of store.

I wish to connect these two tables together so I get the following output 我希望将这两个表连接在一起,所以我得到以下输出

result set 结果集

A.id <- customers id from table
A.customerName
B.id relabelled as store_id
B.StoreName
B.longitude
B.latitude
B.storeAddress

Any ideas guys? 有想法吗?

Thus when I run the query I get the nearest store for each of my customers in my database. 因此,当我运行查询时,我在数据库中得到了每个客户的最近商店。

SELECT latitude, longitude, SQRT(     POW(69.1 * (latitude - [startlat]), 2) +     POW(69.1 * ([startlng] - longitude) * COS(latitude / 57.3), 2)) AS distance FROM TableName1  HAVING distance < 25 ORDER BY distance;

This sql gives you data sorted by nearest location and this question should help you with those two tables How can an SQL query return data from multiple tables . 此sql为您提供按最近位置排序的数据,此问题应可帮助您使用这两个表。SQL查询如何从多个表返回数据 Hope it will help you. 希望对您有帮助。

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

相关问题 获取与表b匹配的表a的所有行 - Gettin all rows of table a that match table b Select 如果另一个表 B 在 mysql 中有行,则表 A 中的所有行 - Select all rows in table A if another table B has rows in mysql 如何从表A中选择与表B不匹配的行(使用时间范围,基于表B) - How to SELECT rows from table A that do not match with table B (using time range, based from table B) 如何删除具有递归结构(MySQL)的表的所有行? - How do I delete all the rows of a table with a recursive structure (MySQL)? MySQL Union-如何在所有行中显示表1的内容? - MySQL union - how do I show table 1 content in all rows? 经度纬度查询,但仅与同一表中的列匹配的行 - Longitude latitude query but only rows that match column in same table 如何从同一表获取基于parent_id的行以与其他行相对应 - How do I get rows to correspond to other rows based on a parent_id , all from the same table MySQL - 如果在另一个表上匹配,则从一个表中排除所有行 - MySQL - Exclude all rows from one table if match on another table 如何编写一个查询,将一个表的每一行与 MySQL 中不同表的所有行进行比较? - How do I write a query that compares each row of one table to all the rows of a different table in MySQL? 如何在 MySQL 中显示一个表中的所有行并从另一个表中计数 - How do I display all rows from a table and count from another table in MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM