嵌套连接隐藏表名

时间:2010-05-17 19:37:58

标签: sql join sqlite

我有三个表:供应商,零件和类型。我需要加入所有这些,同时在三个表中区分具有相同名称的列(例如,“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完成后会忘记表名,但是我怎样才能引用属于表id的列Parts

2 个答案:

答案 0 :(得分:6)

您的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

答案 1 :(得分:0)

只要您根据表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)