[英]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.