Programing

ON 절의 MySQL 알 수없는 열

lottogame 2021. 1. 7. 07:33
반응형

ON 절의 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- 'on clause'의 알 수없는 열 'p.id'

내가 볼 수있는 한 쿼리가 옳아 보이는 한, 무엇이 잘못되었을 수 있는지 아십니까?


ANSI-89 스타일과 ANSI-92 스타일 조인을 혼합하지 마십시오. 그들은 혼란스러운 오류로 이어질 수있는 다른 우선 순위를 가지고 있으며, 이것이 여기서 일어난 일입니다. 귀하의 검색어는 다음과 같이 해석됩니다.

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

위에서 JOIN 키워드를 사용하는 조인은 쉼표 스타일 조인이 고려되기 전에 먼저 평가됩니다. 이 시점에서 테이블 p은 아직 선언되지 않았습니다.

로부터 MySQL의 설명서 :

그러나 쉼표 연산자의 우선 순위는 INNER JOIN, CROSS JOIN, LEFT JOIN 등보다 적습니다. 조인 조건이있을 때 다른 조인 유형과 쉼표 조인을 혼합하면 'on clause'에서 Unknown column 'col_name' 형식의 오류 가 발생할 수 있습니다. 이 문제를 처리하는 방법에 대한 정보는이 섹션의 뒷부분에서 제공됩니다.

항상 ANSI-92 스타일 조인을 사용하는 것이 좋습니다 . 즉, JOIN 키워드를 사용하는 것이 좋습니다 .

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

관련 :


As stated before there is a precedence issue using joins via the comma operator where the LEFT JOIN will be executed and so references to table aliases won't exist at that time. Though you can implicitly tell MySQL to use a JOIN via that statement you may also tell MySQL to evaluate the comma joined tables first, then execute left join thusly:

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

Notice the comma separated tables are contained within parenthesis (). The table aliases and columns will now be available to your other JOINs.


I bumped into this error unknown column, the diff is the query is built thru HQL inside session.executeQuery("select id, name, sum(paid), custType from cust group by brand") that's why having to manually type inner join or join keyword is not an option as the hql is the one generating it. it produces a query sumthing like this:

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

it says "unknown c.custTypeId" column when I am 101% sure it bears that column.

My classes/relations:

Customer {
Integer custId
CustomerType custType
}

CustomerType{
 Integer custTypeId
string code
}

the problem lies in the comma in "from customer, custType" line. it should be with the word JOIN as the answer stated above. but since it is HQL and is being generated, I can't do that. What I did is modified by query and instead of typing select custType, I typed select custType.id, custType.code

I know it's basic but for first timers like me, it was a struggle.

ReferenceURL : https://stackoverflow.com/questions/4065985/mysql-unknown-column-in-on-clause

반응형