2017-09-05 4 views
0

점수와 연도를 기준으로 두 테이블을 결합하여 어떤 ID가 함께 속하는지 찾으려고합니다.MySQL 그룹별로 가입하십시오.

표 A : I는 다음의 예를 만든

id | score | year 
    1  0  2000 
    1  1  2001 
    1  2  2002 

표 B :

id_match | score_match | year 
    10   0   2000 
    10   1   2001 
    10   2   2002 
    20   0   2000 
    20   0   2001 
    20   2   2002 

id_match = 10 일년 1 조작 ID와 동일한 점수 = 갖는다 id_match 용 = 20 반면 그 년 = 2001 년에 다릅니다. 나는 모든 연도에서 정확하게 동일한 점수를 갖는 ID 만 일치시키고 싶습니다.

id | id_match 
1  10 

나는 그것이 비교적 간단한 쿼리 추측을 다음과 같이

출력 테이블은 간단하게 볼 수 있었다. 나는 이런 식의 생각 :

SELECT a.id, b.id_match 
FROM a 
LEFT JOIN b 
    ON a.score = b.score 
    AND a.year = b.year 
GROUP BY a.id, b.id_match; 

그러나, 나는 ID와 id_match의 점수는 모든 년 동안 동일한 경우에만 일치하고 싶습니다.

미리 도움을 청하십시오!

답변

0

난 당신이 원하는 생각 :

SELECT b.id_match 
FROM a JOIN 
    b 
    ON a.year = b.year 
GROUP BY b.id_match 
HAVING SUM(a.score <> b.score_match) = 0; 
0

이 하나

select a.id, b.id_match from tableA a, tableB b 
    where a.score = b.score 
    and a.year = b.year 
    and b.id_match not in (select b.id_match from tableB b, tableA a 
          where b.score != a.score 
           and b.year = a.year 
          group by b.id_match) 
group by a.id, b.id_match; 
시도