MySQL - 操作数应包含 1 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14046838/
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 - Operand should contain 1 column(s)
提问by cdhowie
While working on a system I'm creating, I attempted to use the following query in my project:
在我创建的系统上工作时,我尝试在我的项目中使用以下查询:
SELECT
topics.id,
topics.name,
topics.post_count,
topics.view_count,
COUNT( posts.solved_post ) AS solved_post,
(SELECT users.username AS posted_by,
users.id AS posted_by_id
FROM users
WHERE users.id = posts.posted_by)
FROM topics
LEFT OUTER JOIN posts ON posts.topic_id = topics.id
WHERE topics.cat_id = :cat
GROUP BY topics.id
":cat" is bound by my PHP code as I'm using PDO. 2 is a valid value for ":cat".
":cat" 受我的 PHP 代码约束,因为我正在使用 PDO。2 是 ":cat" 的有效值。
That query though gives me an error: "#1241 - Operand should contain 1 column(s)"
该查询虽然给了我一个错误:“#1241 - 操作数应该包含 1 列”
What stumps me is that I would think that this query would work no problem. Selecting columns, then selecting two more from another table, and continuing on from there. I just can't figure out what the problem is.
难倒我的是,我认为这个查询不会有问题。选择列,然后从另一个表中再选择两个,然后从那里继续。我只是无法弄清楚问题是什么。
Is there a simple fix to this, or another way to write my query?
是否有一个简单的解决方法,或另一种编写查询的方法?
回答by cdhowie
Your subquery is selecting two columns, while you are using it to project one column (as part of the outer SELECT
clause). You can only select one column from such a query in this context.
您的子查询选择两列,而您使用它来投影一列(作为外部SELECT
子句的一部分)。在此上下文中,您只能从此类查询中选择一列。
Consider joining to the users
table instead; this will give you more flexibility when selecting what columns you want from users
.
考虑加入users
表格;这将在选择您想要的列时为您提供更大的灵活性users
。
SELECT
topics.id,
topics.name,
topics.post_count,
topics.view_count,
COUNT( posts.solved_post ) AS solved_post,
users.username AS posted_by,
users.id AS posted_by_id
FROM topics
LEFT OUTER JOIN posts ON posts.topic_id = topics.id
LEFT OUTER JOIN users ON users.id = posts.posted_by
WHERE topics.cat_id = :cat
GROUP BY topics.id
回答by silkfire
This error can also occur if you accidentally use commas instead of AND
in the ON
clause of a JOIN
:
如果您不小心使用逗号而不是AND
在 a 的ON
子句中,也会发生此错误JOIN
:
JOIN joined_table ON (joined_table.column = table.column, joined_table.column2 = table.column2)
^
should be AND, not a comma
回答by jay padaliya
This error can also occur if you accidentally use =
instead of IN
in the WHERE
clause:
如果您不小心在子句中使用了=
代替,也会发生此错误:IN
WHERE
FOR EXAMPLE:
例如:
WHERE product_id = (1,2,3);
回答by Ry-
COUNT( posts.solved_post ) AS solved_post,
(SELECT users.username AS posted_by,
users.id AS posted_by_id
FROM users
WHERE users.id = posts.posted_by)
Well, you can't get multiple columns from one subquery like that. Luckily, the second column is already posts.posted_by
! So:
好吧,您无法从这样的一个子查询中获取多列。幸运的是,第二列已经是posts.posted_by
!所以:
SELECT
topics.id,
topics.name,
topics.post_count,
topics.view_count,
posts.posted_by
COUNT( posts.solved_post ) AS solved_post,
(SELECT users.username AS posted_by_username
FROM users
WHERE users.id = posts.posted_by)
...
回答by iAndy
Another place this error can happen in is assigning a value that has a comma outside of a string. For example:
另一个可能发生此错误的地方是分配一个在字符串外有逗号的值。例如:
SET totalvalue = (IFNULL(i.subtotal,0) + IFNULL(i.tax,0),0)
回答by Jagan Kornana
This error can also occur if you accidentally miss if
function name.
如果您不小心错过了if
函数名称,也会发生此错误。
for example:
例如:
set v_filter_value = 100;
select
f_id,
f_sale_value
from
t_seller
where
f_id = 5
and (v_filter_value <> 0, f_sale_value = v_filter_value, true);
Got this problem when I missed putting ifin the if
function!
当我错过将if放入if
函数时遇到了这个问题!
回答by Moshiur Rahman
(SELECT users.username AS posted_by,
users.id AS posted_by_id
FROM users
WHERE users.id = posts.posted_by)
Here you using sub-query but this sub-query must return only one column. Separate it otherwise it will shows error.
在这里您使用子查询,但此子查询必须仅返回一列。分开它,否则它会显示错误。