例如 - 表名= TABLE
StudentID | SubCode | SubName
-----------------------------
1 M1 Math
1 S1 Science
1 E1 English
2 H1 History
3 G2 Geography
4 M1 Math
我们可以这样显示答案吗?
studentid | SubCode | SubName
---------------------------------
1 M1 Math
1 S1 Science
1 E1 English
答案 0 :(得分:6)
带有IN()
子句的HAVING
子查询应该这样做:
SELECT *
FROM table
WHERE StudentID IN (
SELECT
StudentID
FROM table
GROUP BY userid
HAVING COUNT(*) > 1
)
答案 1 :(得分:2)
SELECT DISTINCT T1.*
FROM YourTable AS T1
INNER JOIN YourTable AS T2
ON T1.StudentID = T2.StudentID
AND T1.SubCode <> T2.SubCode;