修复错误1241操作数应包含1列

时间:2018-11-16 07:48:23

标签: mysql

我对mysql很陌生,当我尝试查询时,它总是显示错误1241 操作数应包含1列

什么会发出此错误?

这是我的查询栏:

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

1 个答案:

答案 0 :(得分:1)

MySQL不允许从SELECT子句中使用的子查询获取多个列。您可以将子查询作为Derived table移至FROM部分,并相应地连接到customer_groups表。

改为使用以下内容:

SELECT g.id, 
       dt.count, 
       dt.total_sales
FROM customer_groups AS g 
JOIN 
(
  SELECT customernumber, 
         COUNT(*) as count, 
         SUM(sales) as total_sales 
  FROM transactions AS t1 
  GROUP BY customernumber
) AS dt 
  ON dt.customernumber BETWEEN g.from_customernumber AND 
                               g.to_customernumber