MySQL“在子句中的未知列”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10483421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL "Unknown Column in On Clause"
提问by Connor Peet
I have the following MySQL query:
我有以下 MySQL 查询:
SELECT posts.id, posts.name, LEFT(posts.content, 400), posts.author, posts.date, users.display_name,
GROUP_CONCAT(tags.tag ORDER BY tag_linking.pid ASC SEPARATOR ",") update_tags
FROM posts, tag_linking, tags
INNER JOIN `users`
ON posts.author=users.id;
WHERE tag_linking.pid = posts.id
AND tags.id = tag_linking.tid
ORDER BY posts.date DESC
Which, was you can see, connects three tables etc. etc. Anyway, the problem is that it gives an error:
哪个,你可以看到,连接三个表等等。无论如何,问题是它给出了一个错误:
ERROR CODE:
SQL Error (1054): Unknown column 'posts.author' in 'on clause'
even though this simpler query used on another page works:
即使在另一个页面上使用的这个更简单的查询有效:
SELECT posts.id, posts.name, LEFT(posts.content, 400), posts.author, posts.date, users.display_name FROM `posts`
INNER JOIN `users`
ON posts.author=users.id
Does anyone have thoughts as to why this is occuring? Thanks for your help.
有没有人有关于为什么会发生这种情况的想法?谢谢你的帮助。
回答by Haim Evgi
because your mix join syntax
因为你的混合连接语法
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.
但是,逗号运算符的优先级小于 INNER JOIN、CROSS JOIN、LEFT JOIN 等。如果在存在连接条件时将逗号连接与其他连接类型混合使用,则可能会出现 Unknown column 'col_name' in 'on clause' 形式的错误。本节稍后将提供有关处理此问题的信息。
the solution is:
解决办法是:
To allow the join to be processed, group the first two tables explicitly with parentheses so that the operands for the ON clause are (t1,t2) and t3:
SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);
Alternatively, avoid the use of the comma operator and use JOIN instead:
SELECT * FROM t1 JOIN t2 JOIN t3 ON (t1.i1 = t3.i3);
要允许处理连接,请使用括号将前两个表显式分组,以便 ON 子句的操作数为 (t1,t2) 和 t3:
SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);
或者,避免使用逗号运算符并使用 JOIN 代替:
SELECT * FROM t1 JOIN t2 JOIN t3 ON (t1.i1 = t3.i3);
回答by Adam Sweeney
is it that semi colon you have ON posts.author=users.id;
你有那个分号吗 ON posts.author=users.id;
回答by Subodh
Giving an alias to your tables in from
clause and surrounding them with parentheses would make it work. But mixing joins is still a bad practice.
在from
子句中为您的表提供别名并用括号将它们括起来会使它起作用。但是混合连接仍然是一种不好的做法。
回答by Falcon
It simply says that author does not exist in post table. either a spelling mistake??
它只是说作者不存在于帖子表中。要么是拼写错误??