我正在尝试从某个半径返回结果的搜索。我有第一步工作,但当我尝试通过包括它停止工作的第二个表来扩展结果时。我不认为UNION是可行的方式,但我想要更好地解释我想要做的事情:
SELECT *, 3956 * 2 * ASIN(SQRT( POWER(SIN((48.4284207 -abs(dest.lat)) * pi()/180 / 2),2) + COS(48.4284207 * pi()/180 ) * COS( abs(dest.lat) * pi()/180) * POWER(SIN((-123.3656444 - dest.lng) * pi()/180 / 2), 2) ))
as distance
FROM business dest having distance < 10500 and
(businessName LIKE '%web%') ORDER BY distance
UNION
(
SELECT b.*
FROM business b, keywords k
WHERE k.keyword
LIKE '%web%' and b.businessID=k.businessID
)
第二个表有两列“关键字”,然后是来自第一个表的businessID的fk。
答案 0 :(得分:1)
根据您评论中的查询,您似乎希望找到某些商家名称中包含'web'或链接关键字表中相关关键字的商家。
如果是这样,这样的查询应该:
SELECT DISTINCT dest.*, 3956 * 2 * ASIN(SQRT( POWER(SIN((48.4284207 -abs(dest.lat)) * pi()/180 / 2),2) + COS(48.4284207 * pi()/180 ) * COS( abs(dest.lat) * pi()/180) * POWER(SIN((-123.3656444 - dest.lng) * pi()/180 / 2), 2) ))
AS distance
FROM business dest
LEFT JOIN keywords k
ON k.businessID = dest.businessID
WHERE dest.businessName LIKE '%web%' OR k.keyword LIKE '%web%'
HAVING distance < 10500
ORDER BY distance