본문 바로가기
MySql

MySQL ERROR 1241 (21000) : 피연산자는 1 개의 열을 포함해야합니다.

by 베이스 공부 2020. 12. 19.
반응형

번호 범위 (고객 번호에서 고객 번호까지)가있는 고객 그룹이 있습니다.

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

이것을 선택하면이 오류가 발생합니다.

ERROR 1241 (21000): Operand should contain 1 column(s)

이 문제를 해결하려면 어떻게해야합니까? 이것에 대한 스레드를 읽었지만 이에 대한 해결책을 찾지 못했습니다.

친애하는!

 

해결 방법

 

MySQL은 하위 쿼리에서 단일 열을 예상합니다. 즉 괄호 안의 SELECT는 단일 열에 대해서만 SELECT 할 수 있습니다.

귀하의 예제에서 두 개의 하위 쿼리를 사용할 수 있습니다. 하나는 개수를 반환하고 다른 하나는 합계를 반환하지만 다음과 같이 쿼리를 다시 작성할 수도 있습니다.

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

 

참조 페이지 https://stackoverflow.com/questions/19861715

 

 

반응형

댓글