Leetcode - 1050. Actors and Directors Who Cooperated At Least Three Times
문제
정답
SELECT actor_id, director_id
FROM (
SELECT actor_id, director_id, COUNT(pair) as counts
FROM (
SELECT actor_id, director_id, CONCAT(actor_id, ",", director_id) AS pair
FROM ActorDirector
) AS New
GROUP BY pair
) AS New2
WHERE counts >= 3
temp table을 두 번 썼는데, 이걸 사용하지 않고 푸는 방법은 없을까? HAVING
clause를 쓰면 더 간단하다.
SELECT actor_id, director_id
FROM ActorDirector
GROUP BY actor_id, director_id
HAVING COUNT(actor_id) >= 3