我有一个sql查询,它在2个表上执行LEFT JOIN,其中一些列是不明确的。我可以为连接的表添加前缀,但是当我尝试从FROM子句中的表中的一个列前缀时,它会告诉我未知列。我甚至尝试给那个表这样的别名......从点AS p和使用“p”来表示前缀,但这也不起作用。我究竟做错了什么?这是我的疑问:
SELECT point_title, point_url, address, city, state, zip_code,
phone, `points`.`lat`, `points`.`longi`, featured,
kmlno, image_url, category.title, category_id,
point_id, lat, longi, reviews.star_points, reviews.review_id,
count(reviews.point_id) as totals
FROM (SELECT *,
( 3959 * acos( cos( radians('37.7717185') ) * cos( radians( lat ) )
* cos( radians( longi ) - radians('-122.4438929') )
+ sin( radians('37.7717185') ) * sin( radians( lat ) ) ) ) AS distance
FROM points
HAVING distance < '25') as distResults
LEFT JOIN category USING ( category_id )
LEFT JOIN reviews USING ( point_id )
WHERE (point_title LIKE '%Playgrounds%' OR category.title LIKE '%Playgrounds%')
GROUP BY point_id
ORDER BY totals DESC, distance
LIMIT 0, 10
以下是所涉及表的架构:
积分表:
字段类型空默认注释
point_id int(11)否
point_title varchar(255)否
category_id int(11)否
说明文字否
point_url varchar(255)否
地址varchar(255)否
city varchar(255)没有
state varchar(255)否
zip_code varchar(255)否
phone varchar(255)没有
filename varchar(255)否
lat varchar(25)没有
longi varchar(25)没有
user_id int(2)否0
特色int(1)否0
status int(1)No 1
kmlno int(2)否0
lastupdate varchar(255)否
image_url varchar(255)否
评论表:
字段类型空默认注释
review_id int(11)否
审查int(1)否0
review_title varchar(255)否
review_desc text否
star_points int(11)否0
user_id int(11)否0
point_id int(11)否
lat varchar(25)没有
longi varchar(25)没有
lastupdate varchar(255)否
status varchar(1)否r
类别表:
字段类型空默认注释
category_id int(11)否
title varchar(255)否
说明文字否
filename varchar(255)否
image_name varchar(255)否
status int(1)否0
lastupdate varchar(255)否
答案 0 :(得分:0)
如果没有看到您的架构,很难确定问题是什么。但我确实看到了一个问题 - 移动距离&lt; 25从内部选择到外部选择。 “距离”名称不适用于内部选择。我也将HAVING改为WHERE,因为没有GROUP BY。
SELECT point_title, point_url, address, city, state, zip_code,
phone, `points`.`lat`, `points`.`longi`, featured,
kmlno, image_url, category.title, category_id,
point_id, lat, longi, reviews.star_points, reviews.review_id,
count(reviews.point_id) as totals
FROM (SELECT * FROM (SELECT *,
( 3959 * acos( cos( radians('37.7717185') ) * cos( radians( lat ) )
* cos( radians( longi ) - radians('-122.4438929') ) +
sin( radians('37.7717185') ) * sin( radians( lat ) ) ) ) AS distance
FROM points) AS distResultsInner
WHERE distance<25)) AS distResults
LEFT JOIN category USING ( category_id )
LEFT JOIN reviews USING ( point_id )
WHERE (point_title LIKE '%Playgrounds%'
OR category.title LIKE '%Playgrounds%')
GROUP BY point_id ORDER BY totals DESC, distance
LIMIT 0 , 10