2010-11-01 59 views
47

我有以下MySQL查詢:MySQL的未知列

SELECT p.*, 
    IF(COUNT(ms.PropertyID) > 0,1,0) AS Contacted, 
    pm.MediaID, 
    date_format(p.AvailableFrom, '%d %b %Y') AS 'AvailableFrom', 
    astext(pg.Geometry) AS Geometry 
FROM property p, propertygeometry pg 
    JOIN shortlist sl ON sl.PropertyID = p.id AND sl.MemberID = 384216 
    LEFT JOIN message ms ON ms.PropertyID = p.id AND ms.SenderID = 384216 
    LEFT JOIN property_media pm ON pm.PropertyID = p.id AND pm.IsPrimary = 1 
WHERE p.paused = 0 
    AND p.PropertyGeometryID = pg.id 
GROUP BY p.id 

而且我得到這個錯誤:

#1054 - 未知列 'p.id' 在「on子句'

據我所見,查詢看起來是正確的,任何想法可能是錯的?

+0

我確定你已經檢查過了,但你在表'property'上確實有一個'id'列,它的別名是p? – 2010-11-01 00:51:41

回答

84

不要混合ANSI-89樣式和ANSI-92樣式連接。它們具有不同的優先級,可能會導致混淆錯誤,這就是發生在這裏的事情。您的查詢被解釋如下:

FROM property p, (
    propertygeometry pg 
    JOIN shortlist sl ON sl.PropertyID = p.id AND sl.MemberID = 384216 
    ... 
) 

在上面,使用JOIN關鍵字的連接首先評估,然後才考慮逗號式連接。此時表p尚未宣佈。

MySQL manual

SELECT p.*, 
    IF(COUNT(ms.PropertyID) > 0,1,0) AS Contacted, 
    pm.MediaID, 
    date_format(p.AvailableFrom, '%d %b %Y') AS 'AvailableFrom', 
    astext(pg.Geometry) AS Geometry 
FROM property p 
    JOIN propertygeometry pg ON p.PropertyGeometryID = pg.id 
    JOIN shortlist sl ON sl.PropertyID = p.id AND sl.MemberID = 384216 
    LEFT JOIN message ms ON ms.PropertyID = p.id AND ms.SenderID = 384216 
    LEFT JOIN property_media pm ON pm.PropertyID = p.id AND pm.IsPrimary = 1 
WHERE p.paused = 0 
GROUP BY p.id 

相關鏈接:

  • 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.

    我使用ANSI-92式的連接,即使用JOIN關鍵字建議總是Why isn't SQL ANSI-92 standard better adopted over ANSI-89?

+0

沒有關於JOIN語法的「隱式」或「顯式」這樣的術語。 – 2010-11-01 00:57:46

+5

優秀的答案。這立即解決了問題,並給了我額外的信息,以幫助我下次。謝謝馬克。 – 2010-11-01 01:25:03

0

我碰到了這個錯誤未知的列,差異是查詢是通過HQL裏面session.executeQuery(「選擇ID,名稱,總和(支付),custType從cust group按品牌」),這就是爲什麼必須手動輸入內部連接或連接關鍵字不是一個選項,因爲hql是生成它的一個選項。 它產生的查詢sumthing這樣的:

select cust_id, name, sum(paid), c.custTypeId 
from customer c, custType ct 
on c.custTypeId = ct.custTypeId 

它說:「未知c.custTypeId」一欄時,我101%肯定它承擔該列。

我的類/關係:

Customer { 
Integer custId 
CustomerType custType 
} 

CustomerType{ 
Integer custTypeId 
string code 
} 

問題出在逗號 「從客戶,custType」 行。應該用JOIN這個詞作爲上面的答案。但由於它是HQL並正在生成,所以我不能那樣做。我所做的是通過查詢進行修改,而不是鍵入select custType,我鍵入select custType.id, custType.code

我知道這是基本的,但對於像我這樣的第一次定時器來說,這是一場鬥爭。

7

如前所述,使用通過逗號運算符連接的優先級問題會執行LEFT JOIN,因此在此時不會引用表別名。雖然你可以暗中告訴MySQL使用通過該語句加入你們也可以告訴MySQL來評價逗號連接的表,然後再執行左連接正是如此:

SELECT p.*, 
IF(COUNT(ms.PropertyID) > 0,1,0) AS Contacted, 
pm.MediaID, 
date_format(p.AvailableFrom, '%d %b %Y') AS 'AvailableFrom', 
astext(pg.Geometry) AS Geometry 
FROM (property p, propertygeometry pg) 
JOIN shortlist sl ON sl.PropertyID = p.id AND sl.MemberID = 384216 
LEFT JOIN message ms ON ms.PropertyID = p.id AND ms.SenderID = 384216 
LEFT JOIN property_media pm ON pm.PropertyID = p.id AND pm.IsPrimary = 1 
WHERE p.paused = 0 
AND p.PropertyGeometryID = pg.id 
GROUP BY p.id 

注意用逗號分隔的表都包含在括號內() 。表別名和列現在可用於其他JOIN。