使用 cos/sin 包括 if/then 和内连接计算半径距离
Calculate radius distance using cos/sin including if/then and inner join
我需要:
我需要在查询中写一些 IF 语句来选择不同的坐标,if table1.business_id
等于 1
.
例如:
从 table1
if table1.business_id = 1
中选择 lng
和 lat
字段,否则,选择 lng
和来自 table2
.
的 lat
个字段
查询:
SELECT *,
(3959 * acos(
cos(radians('.$cords['result']['latitude'].')) *
cos(radians(lat)) * cos( radians(lng) -
radians('.$cords['result']['longitude'].')) +
sin(radians('.$cords['result']['latitude'].')) *
sin(radians(lat))
)) AS distance
FROM table1 INNER JOIN table2 ON table2.id = table1.business_id
WHERE type <> "industry" AND '.$query.'
HAVING distance < '.$radius.'
ORDER BY distance LIMIT 100
我怎样才能让它工作?如何编写技术上正确的查询?
感谢您的回答。
您可以使用 CASE
statement, or an IF
语句来控制流程。这个想法是确定从哪个 table 使用数据:
IF(`table1`.`business_id` = 1, `table1`.`lat`, `table2`.`lat`)
将取代 lat
的所有现有用途(long
也是如此)
SELECT *,
(3959 * acos(
cos(radians('.$cords['result']['latitude'].')) *
cos(radians(IF(`table1`.`business_id` = 1, `table1`.`lat`, `table2`.`lat`))) *
cos( radians(IF(`table1`.`business_id` = 1, `table1`.`lng`, `table2`.`lng`)) -
radians('.$cords['result']['longitude'].')) +
sin(radians('.$cords['result']['latitude'].')) *
sin(radians(IF(`table1`.`business_id` = 1, `table1`.`lat`, `table2`.`lat`)))
)) AS distance
FROM table1 INNER JOIN table2 ON table2.id = table1.business_id
WHERE type <> "industry" AND '.$query.'
HAVING distance < '.$radius.'
ORDER BY distance LIMIT 100
不相关,您似乎在将值串联到查询中。小心不要让自己暴露在 SQL 注入攻击之下!
我想,cos 和 sin 几个转换的计算相当繁重。
我建议先选择一个矩形
where abs(latitude1-latitude2)<latdif
and abs(longitude1-longitude2)<longdif
然后在临时 table、视图或子查询中使用它。
这当然不是问题的完整答案。
'你可以用case when
select a, b,
case
when table1.business_id = 1
then 'Pick lng and lat fields from table1'
else 'pick lng and lat fields from table2'
end col1, clo2
from table1, table 2 where table1.id = table2.id
我需要:
我需要在查询中写一些 IF 语句来选择不同的坐标,if table1.business_id
等于 1
.
例如:
从 table1
if table1.business_id = 1
中选择 lng
和 lat
字段,否则,选择 lng
和来自 table2
.
lat
个字段
查询:
SELECT *,
(3959 * acos(
cos(radians('.$cords['result']['latitude'].')) *
cos(radians(lat)) * cos( radians(lng) -
radians('.$cords['result']['longitude'].')) +
sin(radians('.$cords['result']['latitude'].')) *
sin(radians(lat))
)) AS distance
FROM table1 INNER JOIN table2 ON table2.id = table1.business_id
WHERE type <> "industry" AND '.$query.'
HAVING distance < '.$radius.'
ORDER BY distance LIMIT 100
我怎样才能让它工作?如何编写技术上正确的查询?
感谢您的回答。
您可以使用 CASE
statement, or an IF
语句来控制流程。这个想法是确定从哪个 table 使用数据:
IF(`table1`.`business_id` = 1, `table1`.`lat`, `table2`.`lat`)
将取代 lat
的所有现有用途(long
也是如此)
SELECT *,
(3959 * acos(
cos(radians('.$cords['result']['latitude'].')) *
cos(radians(IF(`table1`.`business_id` = 1, `table1`.`lat`, `table2`.`lat`))) *
cos( radians(IF(`table1`.`business_id` = 1, `table1`.`lng`, `table2`.`lng`)) -
radians('.$cords['result']['longitude'].')) +
sin(radians('.$cords['result']['latitude'].')) *
sin(radians(IF(`table1`.`business_id` = 1, `table1`.`lat`, `table2`.`lat`)))
)) AS distance
FROM table1 INNER JOIN table2 ON table2.id = table1.business_id
WHERE type <> "industry" AND '.$query.'
HAVING distance < '.$radius.'
ORDER BY distance LIMIT 100
不相关,您似乎在将值串联到查询中。小心不要让自己暴露在 SQL 注入攻击之下!
我想,cos 和 sin 几个转换的计算相当繁重。
我建议先选择一个矩形
where abs(latitude1-latitude2)<latdif
and abs(longitude1-longitude2)<longdif
然后在临时 table、视图或子查询中使用它。
这当然不是问题的完整答案。
'你可以用case when
select a, b,
case
when table1.business_id = 1
then 'Pick lng and lat fields from table1'
else 'pick lng and lat fields from table2'
end col1, clo2
from table1, table 2 where table1.id = table2.id