我有MySQL的加入大致有以下幾種形式:MySQL的 - 列沒有發現,儘管沒有被引用在加入
SELECT DISTINCT employees.empl_id,
employees.name,
departments.dep_id,
departments.name,
buildings.id AS building_id,
buildings.address AS building_address
buildings.department AS building_department
FROM employees
LEFT JOIN employees USING (empl_id)
LEFT JOIN departments USING (dep_id)
LEFT JOIN buildings
ON buildings.department = departments.dep_id
當我運行此查詢我得到的錯誤:
ERROR 1054 (42S22) at line 1: Unknown column 'MyDB.employees.dep_id' in 'on clause'
即使艱難列dep_id
真的不存在於員工中,我從來沒有在JOIN中引用employees.dep_id
,而dep_id
是departments
的列,而不是員工!
這可能是由於混合了USING
和ON
子句?
這個JOIN是我寫的腳本的一部分,它使用MySQL版本> 5完成,沒有發生錯誤。我正在修改腳本以適應舊版本的MySQL,並遇到此錯誤。 mysql --version
產量:
mysql Ver 14.7 Distrib 4.1.22, for pc-linux-gnu (i686) using readline 4.3
我通過閱讀this但它並沒有幫助我。任何建議,將不勝感激。
如果您忘記了'USING'的存在,並且堅持用'ON'表示連接,您將得到最佳服務。同樣,不要在沒有特定和故意的理由的情況下使用'DISTINCT'。它經常掩蓋不合邏輯的或無效的查詢,並且可能會損害正確編寫的查詢在使用但不需要時的性能。 –