【问题标题】:Nested joins hide table names嵌套连接隐藏表名
【发布时间】:2010-05-17 19:37:57
【问题描述】:

我有三个表:供应商、零件和类型。我需要在区分三个表中具有相同名称(例如“id”)的列时加入所有这些列。我想成功运行这个查询:

CREATE VIEW Everything AS
SELECT Suppliers.name as supplier, 
       Parts.id, 
       Parts.description, 
       Types.typedesc as type
FROM Suppliers JOIN (Parts JOIN Types ON Parts.type_id = Types.id)
ON Suppliers.id = Parts.supplier_id;

我的 DBMS (sqlite) 抱怨“没有这样的列 (Parts.id)”。我猜它会在 JOIN 完成后忘记表名,但是我怎样才能引用属于表 Parts 的列 id

【问题讨论】:

    标签: sql join sqlite


    【解决方案1】:

    您的 ANSI-92 JOIN 语法不正确 - 使用:

    CREATE VIEW Everything AS
      SELECT Suppliers.name as supplier, 
             Parts.id, 
             Parts.description, 
             Types.typedesc as type
        FROM Suppliers 
        JOIN Parts ON Suppliers.id = Parts.supplier_id
        JOIN Types ON Parts.type_id = Types.id
    

    【讨论】:

      【解决方案2】:

      只要你根据表 Alias.Field 名称来限定连接,你应该没有问题...比如

      CREATE VIEW Everything AS 
            SELECT 
                  Suppliers.name as supplier, 
                  Parts.id, 
                  Parts.description, 
                  Types.typedesc as type 
               FROM 
                  Suppliers,
                  Parts,
                  Types
               WHERE
                      Supplier.ID = Parts.Supplier_ID
                  AND Parts.Type_ID = Types.ID
               ORDER BY
                  (whatever columns)
      

      【讨论】:

      • Ick,隐式连接,请不要鼓励人们使用这种糟糕的编程技术。
      • +1:ANSI-89 连接语法是有效的,只是不是首选(主要是因为没有标准的 OUTER JOIN 语法)。欲了解更多信息:stackoverflow.com/questions/2241991/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 2010-09-28
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多