我在检索具有重复PLAYER_SCORE的PLAYER_SCORE的PLAYER时遇到问题。将PLAYER_SCORE视为重复的条件,其他记录具有相同的P_ID,SCORE_1,SCORE_2,SCORE_3和SCORE_4。
球员表:
+------+---------+ | P_ID | NAME | +------+---------+ | 12 | Juan | | 13 | Miguel | | 14 | Luna | | 15 | Placido | +------+---------+
球员评分表
+-------+------+---------+---------+---------+---------+ | PS_ID | P_ID | SCORE_1 | SCORE_2 | SCORE_3 | SCORE_4 | +-------+------+---------+---------+---------+---------+ | 1 | 1 | 87 | 96 | 79 | 93 | | 2 | 1 | 87 | 96 | 97 | 88 | | 3 | 1 | 87 | 96 | 79 | 93 | | 4 | 2 | 85 | 84 | 85 | 94 | | 5 | 2 | 87 | 96 | 22 | 44 | | 6 | 2 | 85 | 84 | 85 | 94 | | 7 | 3 | 79 | 96 | 82 | 84 | | 8 | 3 | 97 | 96 | 92 | 95 | | 9 | 3 | 87 | 96 | 97 | 87 | | 10 | 4 | 89 | 75 | 99 | 97 | | 11 | 4 | 97 | 96 | 92 | 95 | | 12 | 4 | 87 | 96 | 97 | 87 | +-------+------+---------+---------+---------+---------+
我的sql脚本:
脚本1:
SELECT P.P_ID, NAME FROM PLAYER P
INNER JOIN PLAYER_SCORE PS ON PS.P_ID = P.P_ID
GROUP BY P_ID, SCORE_1, SCORE_2, SCORE_3, SCORE_4
结果:
+------+---------+ | P_ID | NAME | +------+---------+ | 1 | Juan | | 1 | Juan | | 2 | Miguel | | 2 | Miguel | | 3 | Luna | | 3 | Luna | | 3 | Luna | | 4 | Placido | | 4 | Placido | | 4 | Placido | +------+---------+
脚本2:
SELECT P_ID, NAME FROM (
SELECT P.P_ID, NAME FROM PLAYER P
INNER JOIN PLAYER_SCORE PS ON PS.P_ID = P.P_ID
GROUP BY P_ID, SCORE_1, SCORE_2, SCORE_3, SCORE_4
) AS PLAYER GROUP BY P_ID
结果:
+------+---------+ | P_ID | NAME | +------+---------+ | 1 | Juan | | 2 | Miguel | | 3 | Luna | | 4 | Placido | +------+---------+
预期:
+------+--------+ | P_ID | NAME | +------+--------+ | 1 | Juan | | 2 | Miguel | +------+--------+
任何帮助......谢谢。
答案 0 :(得分:1)
您应该只保留重复项,因此请使用HAVING
限制输出:
SELECT P.P_ID, NAME FROM PLAYER P
INNER JOIN PLAYER_SCORE PS ON PS.P_ID = P.P_ID
GROUP BY P_ID, NAME, SCORE_1, SCORE_2, SCORE_3, SCORE_4
HAVING count(*) > 1
答案 1 :(得分:0)
您可以尝试这种方式:
SELECT P_ID, NAME FROM
PLAYER INNER JOIN
(
SELECT P_ID FROM PLAYER_SCORE
GROUP BY P_ID, SCORE_1, SCORE_2, SCORE_3, SCORE_4
HAVING COUNT(*) > 1
) AS SCORE ON SCORE.P_ID = PLAYER.P_ID