【问题标题】:Why does this SQL code give error 1066 (Not unique table/alias: 'user')?为什么这个 SQL 代码给出错误 1066(不是唯一的表/别名:'user')?
【发布时间】:2010-11-28 22:36:51
【问题描述】:

这是我的表结构:

错误信息是:

#1066 - 不是唯一的表/别名:'user'

以下是我的代码。

SELECT article.* , section.title, category.title, user.name, user.name
FROM article
INNER JOIN section ON article.section_id = section.id
INNER JOIN category ON article.category_id = category.id
INNER JOIN user ON article.author_id = user.id
LEFT JOIN user ON article.modified_by = user.id
WHERE article.id = '1'

【问题讨论】:

    标签: sql mysql join mysql-error-1066


    【解决方案1】:

    您需要在第二次加入时给用户表一个别名

    例如

    SELECT article . * , section.title, category.title, user.name, u2.name 
    FROM article 
    INNER JOIN section ON article.section_id = section.id 
    INNER JOIN category ON article.category_id = category.id 
    INNER JOIN user ON article.author_id = user.id 
    LEFT JOIN user u2 ON article.modified_by = u2.id 
    WHERE article.id = '1'
    

    【讨论】:

    • 而且,也许更好:... LEFT JOIN user AS author ON article.author_id = author.id LEFT JOIN user AS modifier ON article.modified_by = modifier.id。换句话说,给user 的两个引用起别名,并赋予它们有意义的名称(尽管在这种情况下我也使用u1u2)。
    • 伙计,这是真正的 INNER JOIN 交易!
    【解决方案2】:
        SELECT art.* , sec.section.title, cat.title, use1.name, use2.name as modifiedby
    FROM article art
    INNER JOIN section sec ON art.section_id = sec.section.id
    INNER JOIN category cat ON art.category_id = cat.id
    INNER JOIN user use1 ON art.author_id = use1.id
    LEFT JOIN user use2 ON art.modified_by = use2.id
    WHERE art.id = '1';
    

    希望这可能会有所帮助

    【讨论】:

      【解决方案3】:

      您在 FROM 子句中提到了两次“用户”。您必须为至少一次提及提供表别名,以便每次提及用户。可以固定到一个或另一个实例:

      FROM article INNER JOIN section
      ON article.section_id = section.id
      INNER JOIN category ON article.category_id = category.id
      INNER JOIN user **AS user1** ON article.author\_id = **user1**.id
      LEFT JOIN user **AS user2** ON article.modified\_by = **user2**.id
      WHERE article.id = '1'
      

      (你可能需要一些不同的东西——我猜到了哪个用户是哪个,但 SQL 引擎不会猜到。)

      另外,也许您只需要一个“用户”。谁知道?

      【讨论】:

        【解决方案4】:

        你的错误是因为你有:

             JOIN user ON article.author_id = user.id
        LEFT JOIN user ON article.modified_by = user.id
        

        您有同一个表的两个实例,但数据库无法确定哪个是哪个。要解决此问题,您需要使用表格别名

             JOIN USER u ON article.author_id = u.id
        LEFT JOIN USER u2 ON article.modified_by = u2.id
        

        总是给你的表起别名是个好习惯,除非你喜欢在没有这种情况的时候一直写完整的表名。

        接下来要解决的问题是:

        SELECT article.* , section.title, category.title, user.name, user.name
        

        1) 永远不要使用SELECT * - 总是拼出你想要的列,即使它是整个表。 Read this SO Question to understand why.

        2) 你会得到与user.name 列相关的不明确的列错误,因为数据库再次无法判断从哪个表实例中提取数据。使用表别名解决了这个问题:

        SELECT article.* , section.title, category.title, u.name, u2.name
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-14
        • 2016-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-08
        相关资源
        最近更新 更多