on 子句中的未知列 {0}
我正在 MySQL 数据库上做一些工作,并完全被收到错误“
1054 - Unknown column 'Puzzles.PuzzleID' in 'on clause'
表名和列名都存在并且正确”所困扰。我刚刚在 Navicat 的视觉设计器中创建了它。
SELECT
`PuzzleCategories`.`PuzzleCategory`,
`Clients`.`BusinessName`,
`Puzzles`.`PuzzleNumber`
FROM
`Puzzles`
Inner Join `PuzzleCategories` ON `Puzzles`.`PuzzleCategoryID` = `PuzzleCategories`.`PuzzleCategoryID` ,
`Clients`
Inner Join `Publications` ON `Clients`.`ClientID` = `Publications`.`ClientID`
Inner Join `PublicationIssues` ON `PublicationIssues`.`PublicationID` = `Publications`.`PublicationID`
Inner Join `PuzzleUsages` ON `PuzzleUsages`.`PuzzleID` = `Puzzles`.`PuzzleID` AND `PuzzleUsages`.`PublicationIssueID` = `PublicationIssues`.`PublicationIssueID`
I am doing some work on a MySQL database and completely stumped on receiving the error
1054 - Unknown column 'Puzzles.PuzzleID' in 'on clause'
Both the table and the column names exist and are correct. I just created it in Navicat's visual designer.
SELECT
`PuzzleCategories`.`PuzzleCategory`,
`Clients`.`BusinessName`,
`Puzzles`.`PuzzleNumber`
FROM
`Puzzles`
Inner Join `PuzzleCategories` ON `Puzzles`.`PuzzleCategoryID` = `PuzzleCategories`.`PuzzleCategoryID` ,
`Clients`
Inner Join `Publications` ON `Clients`.`ClientID` = `Publications`.`ClientID`
Inner Join `PublicationIssues` ON `PublicationIssues`.`PublicationID` = `Publications`.`PublicationID`
Inner Join `PuzzleUsages` ON `PuzzleUsages`.`PuzzleID` = `Puzzles`.`PuzzleID` AND `PuzzleUsages`.`PublicationIssueID` = `PublicationIssues`.`PublicationIssueID`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜
Puzzles
没有列PuzzleID
。该表中的列是否简单地称为ID
?或者Puzzle_ID
?您应该运行 SHOW CREATE TABLE Puzzles 来查看该表的当前定义。
有时,缺少引号可能是罪魁祸首:
上面的代码将查找字面名称为“
Puzzles.PuzzleID
”的列,即,列名长度为 16 个字符,中间有一个点。@Bell 值得获奖,因为他注意到您混合了逗号样式连接和 SQL-92 样式连接。我没注意到!
您不应在同一查询中同时使用两者,因为连接操作的优先级可能会导致混乱。
JOIN
关键字具有更高的优先级。简化您的查询,以便我们可以查看表表达式,它将按如下方式计算:问题是与
PuzzleUsages
的连接需要与Puzzles.PuzzleID
进行比较列,但由于优先级问题,它不能。该列不是最后一个JOIN
操作数的一部分。您可以使用括号来解决错误,显式覆盖表表达式的优先级(就像在算术表达式中使用括号一样):
或者您可以一致地使用 SQL-92
JOIN
语法。我同意@Bell 的观点,这更清楚。I'd guess
Puzzles
doesn't have a columnPuzzleID
. Is the column called simplyID
in that table? OrPuzzle_ID
?You should run
SHOW CREATE TABLE Puzzles
to see the current definition of that table.Sometimes a missing quote can be the culprit:
The above would look for a column literally named "
Puzzles.PuzzleID
," that is, a column name 16 characters long with a dot in the middle.@Bell deserves the prize for noticing that you're mixing comma-style joins and SQL-92 style joins. I didn't notice that!
You shouldn't use both in the same query, because the precedence of join operations is probably causing the confusion.
The
JOIN
keyword has higher precedence. Simplifying your query so we can look at the table-expressions, it would be evaluated as follows:The problem is that the join to
PuzzleUsages
needs to compare to thePuzzles.PuzzleID
column, but because of the precedence issue, it can't. The column is not part of the operands of the lastJOIN
.You can use parentheses to resolve the error, explicitly overriding precedence of table-expressions (just as you would use parentheses in arithmetic expressions):
Or you can just use SQL-92
JOIN
syntax consistently. I agree with @Bell that this is more clear.如果您确定列名正确,则问题可能出在连接的顺序上。听起来逗号两侧的连接是单独构建的。 (我不确定这是否可能,但这是我根据您提供的信息唯一的猜测)
查询可以重组为:
无论如何,它读起来更好。
If you're sure that the column names are right the problem may be from the order of the joins. It sounds like the joins each side of the comma are being built separately. (I'm not sure if this is likely or even possible but it's the only guess I have based on the info you give)
The query could be restructured as:
which just reads better anyway.