我们有MYTABLE的查询,并希望加入OTHER_TABLE以从中获取链接数据。但它似乎没有用。
SELECT *,3956 * 2 * ASIN(SQRT( POWER(SIN(($orig_lat - abs(wlatitude)) * pi()/180 / 2),2)
+ COS($orig_lat * pi()/180 ) * COS(abs(wlatitude) * pi()/180) * POWER(SIN(($orig_lon - wlongitude) * pi()/180 / 2), 2) ))
as distance FROM MYTABLE dest having distance < $dist ORDER BY distance
如何在不出错的情况下将此添加到查询中?
LEFT JOIN OTHER_TABLE ON MYTABLE.column=OTHER_TABLE.column
答案 0 :(得分:1)
无论你做什么,你都不会得到'错误',而是你不需要的结果。
如果连接是1到1,或多对一,则不会得到任何相乘的行。
如果连接是一对多,则右侧的每个对应行数可能会在左侧获得乘法内容,具体取决于您的条件。
请参阅http://en.wikipedia.org/wiki/Relational_algebra#Joins_and_join-like_operators了解连接逻辑。
到您的查询,对于任何其他查询,techincal订单是:
select # ...
from # table
join # table
on # condition
join # another table
on # another condition
where # condition
group # clause
having # condition applied after grouping
order by # ..
答案 1 :(得分:0)
没有深入查询,但它应该是这样的。
SELECT
*,
3956 * 2 * ASIN(SQRT( POWER(SIN(($orig_lat - abs(wlatitude)) * pi()/180 / 2),2) + COS($orig_lat * pi()/180 ) * COS(abs(wlatitude) * pi()/180) * POWER(SIN(($orig_lon - wlongitude) * pi()/180 / 2), 2) ))
as distance
FROM MYTABLE dest
LEFT JOIN OTHER_TABLE ON MYTABLE.column=OTHER_TABLE.column
HAVING distance < $dist
ORDER BY distance
答案 2 :(得分:0)
SELECT *, 3956 * 2 * ASIN(SQRT( POWER(SIN(($orig_lat - abs(wlatitude)) * pi()/180 / 2),2)
+ COS($orig_lat * pi()/180 ) * COS(abs(wlatitude) * pi()/180) * POWER(SIN(($orig_lon - wlongitude) * pi()/180 / 2), 2) )) AS distance
FROM MYTABLE dest
LEFT JOIN OTHER_TABLE ON dest.column = OTHER_TABLE.column
HAVING distance < $dist ORDER BY distance
答案 3 :(得分:0)
使用左连接可能有可能获得重复尝试使用distinct也使用适当的表别名
SELECT DISTINCT
*,
3956 * 2 * ASIN(
SQRT(
POWER(
SIN(
($orig_lat - ABS(dest.wlatitude)) * PI() / 180 / 2
),
2
) + COS($orig_lat * PI() / 180) * COS(ABS(dest.wlatitude) * PI() / 180) * POWER(
SIN(($orig_lon - dest.wlongitude) * PI() / 180 / 2),
2
)
)
) AS distance
FROM
MYTABLE dest
LEFT JOIN OTHER_TABLE o ON dest.column=o.column
HAVING distance < $dist
ORDER BY distance