2012-09-21 2 views
1

두 개의 MySQL 문을 병합하려고합니다.이 문제를 해결하는 데는 가까울 것으로 생각되지만 "알 수없는 열은 'lat'에 있습니다. 'field list' " 아래 굵게 표시된 텍스트를 참조하십시오.MySQL의 # 1054 - '필드 목록'의 알 수없는 열 'lat'

내가 더 효과 "", '', 심지어``와 위도LNG 텍스트를 포괄 시도했습니다.

SELECT post_attributes_3.value AS name, clean, post_attributes.value AS lat, post_attributes_1.value AS lng, post_attributes_2.value AS category, (3959 * acos(cos(radians('%s')) * cos(radians(**lat**)) * cos(radians(**lng**) - radians('%s')) + sin(radians('%s')) * sin(radians(**lat**)))) AS distance 
FROM post_attributes AS post_attributes_3 INNER JOIN ((post_attributes AS post_attributes_1 INNER JOIN (posts INNER JOIN post_attributes ON posts.id = post_attributes.post_id) ON post_attributes_1.post_id = posts.id) INNER JOIN post_attributes AS post_attributes_2 ON posts.id = post_attributes_2.post_id) ON post_attributes_3.post_id = posts.id 
WHERE (((post_attributes_3.name)="name") AND ((post_attributes_2.name)="category_id") AND ((post_attributes.name)="lat") AND ((post_attributes_1.name)="lng")) 
HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20; 

두 SQL 문 제가 아래에 결합했다 :

SELECT name, clean, lat, lng, category, (3959 * acos(cos(radians('%s')) * cos(radians(lat)) * cos(radians(lng) - radians('%s')) + sin(radians('%s')) * sin(radians(lat)))) AS distance FROM categorize HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20; 

SELECT post_attributes_3.value AS name, clean, post_attributes.value AS lat, post_attributes_1.value AS lng, post_attributes_2.value AS category 
FROM post_attributes AS post_attributes_3 INNER JOIN ((post_attributes AS post_attributes_1 INNER JOIN (posts INNER JOIN post_attributes ON posts.id = post_attributes.post_id) ON post_attributes_1.post_id = posts.id) INNER JOIN post_attributes AS post_attributes_2 ON posts.id = post_attributes_2.post_id) ON post_attributes_3.post_id = posts.id 
WHERE (((post_attributes_3.name)="name") AND ((post_attributes_2.name)="category_id") AND ((post_attributes.name)="lat") AND ((post_attributes_1.name)="lng")); 

을이 두 MySQL의 문을가 사용 함께 할 수있는 뭔가를 할 수 있지만 개별적으로 괜찮 AS 문.

업데이트 : 도움 주셔서 감사합니다.하지만이 선택 쿼리가 사용되는 방식으로 인해 MySQL보기를 만들고이를 사용하여 첫 번째 SQL 명령을 실행합니다. 실제 열 이름으로 별칭을 교체

+0

DESCRIBE post_attributes? DESCRIBE를 범주화 하시겠습니까? 모든 테이블 참조에 대해 별명을 가지고 있는지 확인하십시오. 별칭 이름 접두사'post_attributes_2.lat'로 시도하십시오. MySQL은 'lat'를 원하는 테이블 행을 정확히 알아야 할 수도 있습니다. –

+0

post_attributes 테이블에는 post_id, 이름 및 값이 포함되어 있으며이 테이블은 게시물에 대한 다양한 유형의 정보를 저장하는 데 사용되며 그 중 하나는 위도와 경도 값입니다. 카테고리 테이블에 ID와 이름이 포함되어 있습니다. –

+0

누군가가 같은 줄을 따라 대답했습니다. 'categorize AS cat'에 대한 테이블 별칭을 도입하면 중간의 SQL에서 복사 한 모든 열 참조를'name'과 같이'cat.name'으로 바꾸고'cat.lat'을 포함시킵니다. 테이블 별칭 (alias)은 'lng'이라는 컬럼이 여러 개있을 때 어떤 데이터를 참조하는지 명확하게하기 위해 필요합니다. –

답변

1

봅니다 :

SELECT post_attributes_3.value AS name, clean, post_attributes.value AS lat, post_attributes_1.value AS lng, post_attributes_2.value AS category, (3959 * acos(cos(radians('%s')) * cos(radians(post_attributes.value)) * cos(radians(post_attributes_1.value) - radians('%s')) + sin(radians('%s')) * sin(radians(post_attributes.value)))) AS distance 
FROM post_attributes AS post_attributes_3 INNER JOIN ((post_attributes AS post_attributes_1 INNER JOIN (posts INNER JOIN post_attributes ON posts.id = post_attributes.post_id) ON post_attributes_1.post_id = posts.id) INNER JOIN post_attributes AS post_attributes_2 ON posts.id = post_attributes_2.post_id) ON post_attributes_3.post_id = posts.id 
WHERE (((post_attributes_3.name)="name") AND ((post_attributes_2.name)="category_id") AND ((post_attributes.name)="lat") AND ((post_attributes_1.name)="lng")) 
HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20; 

SQL fiddle to show how to use WHERE and HAVING with column aliases

마지막 쿼리는이 너무 발생한 문제이며, 오류가 발생합니다.

+0

감사합니다. Jocelyn, 오류를 반환하지 않습니다. 희망 사항이 있습니다. –

+0

데이터베이스에서 데이터를 가져올 수없는 것처럼 보일 수도 있습니다. 다음 링크는 첫 번째 SQL 문을 사용하고 있습니다. http://www.local-social.co.uk/phpsqlsearch_genxml.php?lang=54&lng=-1.57&radius=2500000 이 새 파일에서 select 명령을 변경했지만 아무 것도 반환하지 않습니다. http : // www .local-social.co.uk/phpsqlsearch-xml.php? lat = 54 & lng = -1.57 & radius = 2500000 –

관련 문제