2017-05-27 24 views
1

這裏是我的SQL查詢:爲什麼我得到這個錯誤?語法錯誤或訪問衝突:1066不是唯一的表/別名:「員工」

SELECT Employees.employee_id AS `Employees__employee_id`, 
    Employees.designation_id AS `Employees__designation_id`, 
    Employees.first_name AS `Employees__first_name`, 
    Employees.last_name AS `Employees__last_name`, 
    Employees.address AS `Employees__address`, 
    Employees.contact_no AS `Employees__contact_no`, 
    Employees.joining_date AS `Employees__joining_date`, 
    Employees.username AS `Employees__username`, 
    Employees.password AS `Employees__password`, 
    Employees.basic_pay AS `Employees__basic_pay`, 
    Employees.create_date AS `Employees__create_date`, 
    Employees.status AS `Employees__status`, 
    Designations.designation_id AS `Designations__designation_id`, 
    Designations.designation_name AS `Designations__designation_name`, 
    Designations.description AS `Designations__description` 
FROM employees Employees 
INNER JOIN employees Employees 
    ON Employees.employee_id = (Employees.employee_id) 
INNER JOIN designations Designations 
    ON Designations.designation_id = (Employees.designation_id) 
LIMIT 20 OFFSET 0 

我得到的錯誤是:

Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Employees'!

你能不能幫我在這。

回答

0

對於查詢中的多個表,不能使用相同的別名(Employees)。在這裏,您在fromjoin子句中都使用它。只要在其中一個別名中使用不同的別名,你應該沒問題。

+0

Thanx pro。我如何編輯cakephp中的查詢? –

0

在你FROM條款你設置一個表的別名Employees一樣長OK爲你的MySQL在INNER JOIN條款運行大小寫敏感(在Linux或Unix等),或者如果您在Windows上運行它可能會失敗,但後來你使用相同的別名加入表格。這是一個重複的別名。 SQL不允許出現重複的別名,否則,例如,在調用Employees.designation_id時,它不會從應該評估哪個表中知道表達式。

無論您是否加入相同的表格,主表格和連接表格的表別名都必須不同。

更新:也許你不需要再次加入同一張表。刪除inner join employees部分:

SELECT Employees.employee_id AS `Employees__employee_id`, 
    Employees.designation_id AS `Employees__designation_id`, 
    Employees.first_name AS `Employees__first_name`, 
    Employees.last_name AS `Employees__last_name`, 
    Employees.address AS `Employees__address`, 
    Employees.contact_no AS `Employees__contact_no`, 
    Employees.joining_date AS `Employees__joining_date`, 
    Employees.username AS `Employees__username`, 
    Employees.password AS `Employees__password`, 
    Employees.basic_pay AS `Employees__basic_pay`, 
    Employees.create_date AS `Employees__create_date`, 
    Employees.status AS `Employees__status`, 
    Designations.designation_id AS `Designations__designation_id`, 
    Designations.designation_name AS `Designations__designation_name`, 
    Designations.description AS `Designations__description` 
FROM employees Employees 

INNER JOIN designations Designations 
    ON Designations.designation_id = (Employees.designation_id) 
LIMIT 20 OFFSET 0 
+0

感謝兄弟..但我怎麼能編輯查詢的情況下cakephp?! –

+0

@dif_side - 已更新。 –

相關問題