on 子句中的未知列 {0}

发布于 2024-08-12 04:09:01 字数 765 浏览 4 评论 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 技术交流群。

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

发布评论

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

评论(2

秋心╮凉 2024-08-19 04:09:01

我猜 Puzzles 没有列 PuzzleID。该表中的列是否简单地称为 ID?或者Puzzle_ID

您应该运行 SHOW CREATE TABLE Puzzles 来查看该表的当前定义。

有时,缺少引号可能是罪魁祸首:

... ON `Puzzles.PuzzleID` ...

上面的代码将查找字面名称为“Puzzles.PuzzleID”的列,即,列名长度为 16 个字符,中间有一个点。


@Bell 值得获奖,因为他注意到您混合了逗号样式连接和 SQL-92 样式连接。我没注意到!

您不应在同一查询中同时使用两者,因为连接操作的优先级可能会导致混乱。

JOIN 关键字具有更高的优先级。简化您的查询,以便我们可以查看表表达式,它将按如下方式计算:

SELECT . . . 
FROM (Puzzles JOIN PuzzleCategories),
(Clients JOIN Publications JOIN PublicationIssues JOIN PuzzleUsages)

问题是与 PuzzleUsages 的连接需要与 Puzzles.PuzzleID 进行比较列,但由于优先级问题,它不能。该列不是最后一个 JOIN 操作数的一部分。

您可以使用括号来解决错误,显式覆盖表表达式的优先级(就像在算术表达式中使用括号一样):

SELECT . . . 
FROM Puzzles JOIN (PuzzleCategories, Clients)
JOIN Publications JOIN PublicationIssues JOIN PuzzleUsages

或者您可以一致地使用 SQL-92 JOIN 语法。我同意@Bell 的观点,这更清楚。

SELECT . . . 
FROM Puzzles JOIN PuzzleCategories JOIN Clients
JOIN Publications JOIN PublicationIssues JOIN PuzzleUsages

I'd guess Puzzles doesn't have a column PuzzleID. Is the column called simply ID in that table? Or Puzzle_ID?

You should run SHOW CREATE TABLE Puzzles to see the current definition of that table.

Sometimes a missing quote can be the culprit:

... ON `Puzzles.PuzzleID` ...

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:

SELECT . . . 
FROM (Puzzles JOIN PuzzleCategories),
(Clients JOIN Publications JOIN PublicationIssues JOIN PuzzleUsages)

The problem is that the join to PuzzleUsages needs to compare to the Puzzles.PuzzleID column, but because of the precedence issue, it can't. The column is not part of the operands of the last JOIN.

You can use parentheses to resolve the error, explicitly overriding precedence of table-expressions (just as you would use parentheses in arithmetic expressions):

SELECT . . . 
FROM Puzzles JOIN (PuzzleCategories, Clients)
JOIN Publications JOIN PublicationIssues JOIN PuzzleUsages

Or you can just use SQL-92 JOIN syntax consistently. I agree with @Bell that this is more clear.

SELECT . . . 
FROM Puzzles JOIN PuzzleCategories JOIN Clients
JOIN Publications JOIN PublicationIssues JOIN PuzzleUsages
盛夏尉蓝 2024-08-19 04:09:01

如果您确定列名正确,则问题可能出在连接的顺序上。听起来逗号两侧的连接是单独构建的。 (我不确定这是否可能,但这是我根据您提供的信息唯一的猜测)

查询可以重组为:


SELECT
`PuzzleCategories`.`PuzzleCategory`,
`Clients`.`BusinessName`,
`Puzzles`.`PuzzleNumber`
FROM
`Clients`
INNER JOIN `Publications` ON `Clients`.`ClientID` = `Publications`.`ClientID`
INNER JOIN `PublicationIssues` ON `PublicationIssues`.`PublicationID` =   `Publications`.`PublicationID`
INNER JOIN `PuzzleUsages` ON`PuzzleUsages`.`PublicationIssueID` = `PublicationIssues`.`PublicationIssueID`
INNER JOIN `Puzzles` ON `Puzzles`.`PuzzleID` = `PuzzleUsages`.`PuzzleID`
INNER JOIN `PuzzleCategories` ON `Puzzles`.`PuzzleCategoryID` = `PuzzleCategories`.`PuzzleCategoryID`

无论如何,它读起来更好。

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:


SELECT
`PuzzleCategories`.`PuzzleCategory`,
`Clients`.`BusinessName`,
`Puzzles`.`PuzzleNumber`
FROM
`Clients`
INNER JOIN `Publications` ON `Clients`.`ClientID` = `Publications`.`ClientID`
INNER JOIN `PublicationIssues` ON `PublicationIssues`.`PublicationID` =   `Publications`.`PublicationID`
INNER JOIN `PuzzleUsages` ON`PuzzleUsages`.`PublicationIssueID` = `PublicationIssues`.`PublicationIssueID`
INNER JOIN `Puzzles` ON `Puzzles`.`PuzzleID` = `PuzzleUsages`.`PuzzleID`
INNER JOIN `PuzzleCategories` ON `Puzzles`.`PuzzleCategoryID` = `PuzzleCategories`.`PuzzleCategoryID`

which just reads better anyway.

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