简体   繁体   English

Mysql ERROR 1241(21000):操作数应包含1列

[英]Mysql ERROR 1241 (21000): Operand should contain 1 column(s)

I have Customer Groups with Number-Ranges (from Customernumber, to 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! 最好的祝福!

MySQL is expecting a single column from your subquery, ie 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM