MySQL ON 子句中的未知列

发布于 2024-09-30 01:11:04 字数 671 浏览 3 评论 0原文

我有以下 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'

据我所知,查询看起来是正确的,任何想法可能出什么问题了?

I have the following MySQL query:

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

And I'm getting this error:

#1054 - Unknown column 'p.id' in 'on clause'

As far as I can see the query looks right, any idea what could be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黯然 2024-10-07 01:11:04

不要混合使用 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' 中的未知列 '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

相关:

Don't mix ANSI-89 style and ANSI-92 style joins. They have different precedence which can lead to confusing errors, and that is what has happened here. Your query is being interpreted as follows:

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

In the above, the joins using the JOIN keyword are evaluated first before the comma-style join is even considered. At that point the table p isn't yet declared.

From the MySQL manual:

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.

I'd recommend always using ANSI-92 style joins, i.e. using the JOIN keyword:

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

Related:

相思故 2024-10-07 01:11:04

如前所述,通过逗号运算符使用联接存在优先级问题,其中将执行 LEFT JOIN,因此此时对表别名的引用将不存在。虽然您可以通过该语句隐式告诉 MySQL 使用 JOIN,但您也可以告诉 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 使用。

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.

最舍不得你 2024-10-07 01:11:04

我遇到了这个错误未知列,差异是查询是通过 session.executeQuery("select id, name, sum(paid), custType from cust group by Brand") 中的 HQL 构建的,这就是为什么必须手动键入内部联接或join 关键字不是一个选项,因为 hql 是生成它的选项。
它会生成这样的查询总和:

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

当我 101% 确定它包含该列时,它会显示“未知的 c.custTypeId”列。

我的类/关系:

Customer {
Integer custId
CustomerType custType
}

CustomerType{
 Integer custTypeId
string code
}

问题出在“from customer, custType”行中的逗号。应该与上述答案中的 JOIN 一词一起使用。但由于它是 HQL 并且正在生成,所以我不能这样做。我所做的是通过查询进行修改,我没有输入 select custType,而是输入了 select custType.id, custType.code

我知道这很基本,但对于像我这样的初学者来说,这是一场斗争。

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.

丑疤怪 2024-10-07 01:11:04

以防万一有人像这样搬起石头砸自己的脚。

我是 MySQL Workbench 的新手,正在测试新模式,但我并没有想到在推送到服务器进行测试时选择添加 DROP 命令。

这意味着当我调整表格并更新视图时,我不断收到这些奇怪的错误。

长话短说,确保在早期测试阶段每次推送时都实际更新服务器上的表架构,否则您的新视图可能会因为服务器上的旧表而失败。

Just in case someone else shoots themselves in the foot like this.

I was new to MySQL Workbench and testing a new schema, I however didn't think to select to add the DROP commands while pushing to the server for testing.

Which meant that when I tweaked my tables and updated the view, I kept getting these weird errors.

Long story short make sure you are actually updating the table schema on the server on every push while in the early testing phase, otherwise your new view may be failing because of the old tables on the server.

苏别ゝ 2024-10-07 01:11:04

如果这对某人有帮助(以及对未来我自己的注释),我在尝试在 MariaDB 中执行以下查询时遇到此错误:

SELECT a.name, b.name
FROM `cities` as a
INNER JOIN `countries` as b 
ON `a.country_id` = `b`.`id`;

而我应该这样写:

SELECT a.name, b.name
FROM `cities` as a
INNER JOIN `countries` as b 
ON `a`.`country_id` = `b`.`id`;

我将把它留给读者来发现差异锻炼。 :)

If this helps someone (and a note to future myself), I was getting this error when trying to execute the following queries in MariaDB:

SELECT a.name, b.name
FROM `cities` as a
INNER JOIN `countries` as b 
ON `a.country_id` = `b`.`id`;

whereas I should have written it like:

SELECT a.name, b.name
FROM `cities` as a
INNER JOIN `countries` as b 
ON `a`.`country_id` = `b`.`id`;

I'll leave it to the reader to spot the difference as an exercise. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文