Mysql ERROR 1241 (21000): 操作数应包含 1 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19861715/
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 ERROR 1241 (21000): Operand should contain 1 column(s)
提问by Marco
I have Customer Groups with Number-Ranges (from Customernumber, to Customernumber).
我有具有编号范围的客户组(从 Customernumber 到 Customernumber)。
select g.id,
(select count(*), sum(sales)
FROM transactions t1
where t1.customernumber between g.from_customernumber and g.to_customernumber)
from customer_groups g
When selecting this i get this error
选择此选项时,我收到此错误
ERROR 1241 (21000): Operand should contain 1 column(s)
What can i do to fix this? I've read some threads about this but I didn't find a solution for this.
我能做些什么来解决这个问题?我已经阅读了一些关于此的主题,但我没有找到解决方案。
Best regards!
此致!
回答by fthiella
MySQL is expecting a single column from your subquery, i.e. the SELECT in the brackets can only SELECT for a single column.
MySQL 期望来自您的子查询的单个列,即括号中的 SELECT 只能对单个列进行 SELECT。
In your example, you could use two subqueries, one that returns the count and one other that returns the sum, but you could also rewrite your query as this:
在您的示例中,您可以使用两个子查询,一个返回计数,另一个返回总和,但您也可以将查询重写为:
SELECT g.id, COUNT(t1.customernumber), SUM(sales)
FROM
customer_groups g LEFT JOIN transactions t1
ON t1.customernumber between g.from_customernumber and g.to_customernumber