2010-05-17 79 views
4

我有三個表:供應商,零件和類型。我需要加入他們所有的人,同時區分三個表中同名的列(比如「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

回答

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 
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) 
+0

Ick,暗示加入,請不要鼓勵人們使用這種可憐的編程技術。 – HLGEM 2010-05-17 19:44:32

+0

+1:ANSI-89連接語法有效,但它不是首選(主要是因爲沒有標準的OUTER JOIN語法)。欲瞭解更多信息:http://stackoverflow.com/questions/2241991/in-mysql-queries-why-use-join-instead-of-where – 2010-05-17 20:01:47