2014-03-29 29 views
0

我想將2個表連接到另一個表。我有這樣的SQL查詢:MySQL錯誤:'on子句'中未知列'tablename.colname

SELECT 
desc_1.description, 
desc_2.description, 
Object.objID, 
Item.itemID 
FROM 
Object, 
Item 
INNER JOIN Foo desc_1 ON desc_1.descID = Object.objDescID 
INNER JOIN Foo desc_2 ON desc_2.descID = Item.itemDescID; 

但是我得到以下錯誤:

ERROR 1054: Unkown column 'Object.objDescID' in 'on clause' 

所需的輸出:

description | description | objID | itemID | 
------------+-------------+-------+--------+  
"Daughter" | "Shoes" | 20 | 25 | 

有誰知道爲什麼發生這種情況?列中存在我檢查拼寫等。

回答

0

如果查詢你想要什麼,然後更換,cross join

SELECT desc_1.description, desc_2.description, 
     Object.objID, Item.itemID 
FROM Object CROSS JOIN 
    Item INNER JOIN 
    Foo desc_1 
    ON desc_1.descID = Object.objDescID INNER JOIN 
    Foo desc_2 
    ON desc_2.descID = Item.itemDescID; 

documentation即使解釋了這一點,雖然我承認這是一個有點難找:

However, the precedence of the comma operator is less than of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.

1

您不能混合遺留隱式連接語法和顯式連接語法。使用

SELECT desc_1.description, 
     desc_2.description, 
     Object.objID, 
     Item.itemID 
FROM Object 
INNER JOIN Item on { fill in the column that relate object and item } 
INNER JOIN Foo desc_1 ON desc_1.descID = Object.objDescID 
INNER JOIN Foo desc_2 ON desc_2.descID = Item.itemDescID; 
+0

項目和對象不相關。對象從Foo獲取它的描述,Item也是如此。對象和項目都與Foo相關,但不相互關聯。 – JH95

+1

也許你應該將示例數據和所需的輸出一起添加到你的問題中。 –

相關問題