2012-09-21 59 views
1

我試圖合併兩個MySQL語句,我想我已經接近解決這個問題了,但我得到了「#1054 - 未知列'lat' '字段列表'「參考下面的粗體文本。MySQL#1054 - '字段列表'中的未知列'lat'

我試着涵蓋LATLNG文本用 「」, '',甚至``沒有效果。

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,名稱和值,此表用於存儲關於帖子的不同類型的信息,其中之一是lat和lng值。 類別表包含ID和名稱。 –

+0

有人從相同的路線回答。引入一個用於「分類AS貓」的表別名,然後將從中間SQL(例如'name')複製的每個列引用替換爲'cat.name',這將包括'cat.lat'。需要使用表別名來消除在播放中可能存在多個名爲'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?lat=54&lng=-1.57&radius=2500000 我改變了這個新文件中的select命令,但它沒有返回任何東西: http:// www 。本地-social.co.uk/phpsqlsearch-xml.php?LAT = 54&LNG = -1.57&半徑= 2500000 –

相關問題