2017-09-05 5 views
0

どのIDが一緒に属しているかを見つけるために、スコアと年に基づいて2つのテーブルを照合しようとしています。それは20 =MySQLグループで結合する

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 id_matchために対し通年1for = IDと同じスコアを有する:

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

表B:

表:私は、次の例を作っ年= 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

はこの1

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; 
をお試しください
関連する問題