如何根据 table(距离为 10)与 tables 匹配多行 table A 和多行 table B
How to match multiple rows of table A with multiple rows of table B based on lat/lng(by distance 10) from those tables
select tableA.*,tableB.*,
111.045* DEGREES(ACOS(COS(RADIANS(tableA.latitude)) * COS(RADIANS(tableB.latitude)) * COS(RADIANS(tableA.longitude) -
RADIANS(tableB.longitude)) + SIN(RADIANS(tableA.latitude)) * SIN(RADIANS(tableB.latitude)))) AS distance_in_km
from table A
join(select tableB.latitude,tableB.longitude from tableB)
on tableA.id = tableB.id HAVING distance <10
此查询有效,但它只获取 table A 中的第一行并将其与 table B 中的行进行比较。但我希望 lat/lng 中的每一行 table A 应该与 table B.
中的所有 lat/lng 进行比较
请尝试 LEFT JOIN
而不是 JOIN
。希望它会成功。
为了将表 A 中的每一行与表 B 中的每一行连接起来,您必须进行交叉连接,即
select tableA.*,tableB.*,
111.045* DEGREES(ACOS(COS(RADIANS(tableA.latitude)) * COS(RADIANS(tableB.latitude)) * COS(RADIANS(tableA.longitude) -
RADIANS(tableB.longitude)) + SIN(RADIANS(tableA.latitude)) * SIN(RADIANS(tableB.latitude)))) AS distance_in_km
from tableA
cross join tableB
HAVING distance <10
select tableA.*,tableB.*,
111.045* DEGREES(ACOS(COS(RADIANS(tableA.latitude)) * COS(RADIANS(tableB.latitude)) * COS(RADIANS(tableA.longitude) -
RADIANS(tableB.longitude)) + SIN(RADIANS(tableA.latitude)) * SIN(RADIANS(tableB.latitude)))) AS distance_in_km
from table A
join(select tableB.latitude,tableB.longitude from tableB)
on tableA.id = tableB.id HAVING distance <10
此查询有效,但它只获取 table A 中的第一行并将其与 table B 中的行进行比较。但我希望 lat/lng 中的每一行 table A 应该与 table B.
中的所有 lat/lng 进行比较请尝试 LEFT JOIN
而不是 JOIN
。希望它会成功。
为了将表 A 中的每一行与表 B 中的每一行连接起来,您必须进行交叉连接,即
select tableA.*,tableB.*,
111.045* DEGREES(ACOS(COS(RADIANS(tableA.latitude)) * COS(RADIANS(tableB.latitude)) * COS(RADIANS(tableA.longitude) -
RADIANS(tableB.longitude)) + SIN(RADIANS(tableA.latitude)) * SIN(RADIANS(tableB.latitude)))) AS distance_in_km
from tableA
cross join tableB
HAVING distance <10