MySQL ON 子句中的未知列
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要混合使用 ANSI-89 样式和 ANSI-92 样式连接。它们具有不同的优先级,这可能会导致令人困惑的错误,这就是这里发生的情况。您的查询将被解释如下:
在上面,在考虑逗号样式连接之前,首先评估使用 JOIN 关键字的连接。此时表
p
尚未声明。来自 MySQL 手册:
我建议始终使用 ANSI-92 样式连接,即使用 JOIN 关键字:
相关:
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:
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:
I'd recommend always using ANSI-92 style joins, i.e. using the JOIN keyword:
Related:
如前所述,通过逗号运算符使用联接存在优先级问题,其中将执行 LEFT JOIN,因此此时对表别名的引用将不存在。虽然您可以通过该语句隐式告诉 MySQL 使用 JOIN,但您也可以告诉 MySQL 首先评估逗号连接的表,然后执行左连接:
请注意,逗号分隔的表包含在括号 () 内。表别名和列现在可供其他 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:
Notice the comma separated tables are contained within parenthesis (). The table aliases and columns will now be available to your other JOINs.
我遇到了这个错误未知列,差异是查询是通过 session.executeQuery("select id, name, sum(paid), custType from cust group by Brand") 中的 HQL 构建的,这就是为什么必须手动键入内部联接或join 关键字不是一个选项,因为 hql 是生成它的选项。
它会生成这样的查询总和:
当我 101% 确定它包含该列时,它会显示“未知的 c.custTypeId”列。
我的类/关系:
问题出在“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:
it says "unknown c.custTypeId" column when I am 101% sure it bears that column.
My classes/relations:
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.
以防万一有人像这样搬起石头砸自己的脚。
我是 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.
如果这对某人有帮助(以及对未来我自己的注释),我在尝试在 MariaDB 中执行以下查询时遇到此错误:
而我应该这样写:
我将把它留给读者来发现差异锻炼。 :)
If this helps someone (and a note to future myself), I was getting this error when trying to execute the following queries in MariaDB:
whereas I should have written it like:
I'll leave it to the reader to spot the difference as an exercise. :)