如何在没有行重复的情况下获得结果

时间:2020-01-29 14:51:30

标签: mysql sql

请,如何在没有所有重复项的情况下执行此自我加入?

选择t1.subject_id,t1.score,t2.score,从hs_results AS t1内联接hs_results AS t2 ON t1.subject_id = t2.subject_id t1.term = 1 AND t2.term = 2 AND t1.admin_no ='989'AND YEAR = 2020

enter image description here

 Base TABLE
R_id    Admin_No    Subject_id  score   Class_id     semester   YEAR
1       989          23         90       2            1        2020
2       989          21         60       2            1        2020
3       989          24         78       2            1        2020
4       989          23         100      2            2        2020
5       989          21         81       2            2        2020
6       989          24         87       2            2        2020

desired result AFTER self JOIN
R_id    Admin_no    Subject_id  t1.score    t2.score    Class_id    YEAR
1        989        23           90           100        2           2020
2        989        21           60           81         2           2020
3        989        24           78           87         2           2020

1 个答案:

答案 0 :(得分:0)

我不确定您在说什么重复。但是您的JOIN似乎需要更多条件:

SELECT t1.subject_id, t1.score, t2.score, . . .
FROM hs_results t1 INNER JOIN
     hs_results t2
     ON t1.admin_no = t2.admin_no AND
        t1.subject_id = t2.subject_id AND
        t1.YEAR = t2.YEAR
WHERE t1.term = 1 AND
      t2.term = 2 AND
      t1.admin_no = '989' AND
      t1.YEAR = 2020;