我試圖合併兩個MySQL語句,我想我已經接近解決這個問題了,但我得到了「#1054 - 未知列'lat' '字段列表'「參考下面的粗體文本。MySQL#1054 - '字段列表'中的未知列'lat'
我試着涵蓋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命令到我的第一個。
DESCRIBE post_attributes? DESCRIBE分類?確保您具有所有表格引用的別名。嘗試使用別名名稱前綴'post_attributes_2.lat' MySQL可能需要確切地知道哪個錶行需要'lat'。 –
post_attributes表包含post_id,名稱和值,此表用於存儲關於帖子的不同類型的信息,其中之一是lat和lng值。 類別表包含ID和名稱。 –
有人從相同的路線回答。引入一個用於「分類AS貓」的表別名,然後將從中間SQL(例如'name')複製的每個列引用替換爲'cat.name',這將包括'cat.lat'。需要使用表別名來消除在播放中可能存在多個名爲'lng'的列時所指的數據。 –