Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 526
Marco Avatar asked Nov 08 '13 14:11

Marco


People also ask

What is error operand should contain 1 column s?

The error Operand should contain 1 column(s) is most likely caused by a subquery that's returning more than one column.

What is error code 1241 in MySQL?

MySQL error 1241: Operand should contain 1 column(s)


1 Answers

MySQL is expecting a single column from your subquery, i.e. the SELECT in the brackets can only SELECT for a single column.

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
like image 165
fthiella Avatar answered Oct 12 '22 22:10

fthiella