Self Right
Self Right

Reputation: 11

fixing error 1241 operand should contain 1 column(s)

I am very new to mysql and when i try my query it always gives and error 1241 Operand should contain 1 column(s)

what gives off this error?

here is my block of query:

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

Upvotes: 1

Views: 383

Answers (1)

Madhur Bhaiya
Madhur Bhaiya

Reputation: 28834

MySQL does not allow getting more than one column from a subquery, used within SELECT clause. You can rather shift your subquery to FROM part as a Derived table, and join to the customer_groups table accordingly.

Use the following instead:

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

Upvotes: 2

Related Questions