【问题标题】:Why do I get this error? Syntax error or access violation: 1066 Not unique table/alias: 'Employees'为什么我会收到此错误?语法错误或访问冲突:1066 不是唯一的表/别名:“员工”
【发布时间】:2017-10-28 05:17:39
【问题描述】:

这是我的 SQL 查询:

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

我得到的错误是:

错误:SQLSTATE[42000]:语法错误或访问冲突:1066 不是 唯一表/别名:'Employees'!

你能帮帮我吗?

【问题讨论】:

    标签: php mysql cakephp


    【解决方案1】:

    在您的FROM 子句中,您设置了一个表别名Employees,只要您的 MySQL 运行区分大小写(如在 Linux 或 Unix 上)就可以,或者如果您在 Windows 上运行它可能会失败,但稍后在您的 INNER JOIN 子句您使用相同的别名加入表。这是一个重复的别名。 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
    

    【讨论】:

    • 谢谢兄弟.. 但是如果是 cakephp,我该如何编辑查询?!
    【解决方案2】:

    您不能对查询中的多个表使用相同的别名 (Employees)。在这里,您在 fromjoin 子句中都使用了它。只需在其中一个中使用不同的别名就可以了。

    【讨论】:

      猜你喜欢
      • 2018-12-07
      • 2019-03-15
      • 2016-02-06
      • 2015-04-01
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      相关资源
      最近更新 更多