Post

Subquery IN vs = ; error code 1242

Subquery IN vs = ; error code 1242

Error Code: 1242. Subquery returns more than 1 row

에러 나는 쿼리

1
select title from film where film_id = (select film_id from film_category where category_id = (select category_id from category where name = 'Action'))
  • 이 쿼리에서 WHERE film_id = (subquery) 는 = 연산자를 사용하고 있다.

    film_id 는 하나의 값과만 비교할 수 있다는 뜻. 그런데 서브쿼리가 여러 film_id를 반환하기 때문에 오류

수정

1
2
3
4
select title
from film
where film_id in(select film_id from film_category where category_id = (select category_id from category where name = 'Action'))
;

IN 사용 예

1
select first_name, last_name, email from customer where customer_id IN (select customer_id from rental);

✅ 차이 요약

= (equal) : ✅ 단일 값 ❌ 여러 값 IN (…) : ❌ 단일 값 ✅ 여러 값

This post is licensed under CC BY 4.0 by the author.