这是我的选择陈述:
SELECT lastName + ',' + firstName as Name,
classNumber
FROM STUDENT
RIGHT JOIN ENROLL ON Enroll.stuid = Student.stuid
和输出:
name classnumber
--------------------------
Smith,Tom ART103A
Smith,Tom HST205A
Chin,Ann ART103A
Chin,Ann CSC201A
Chin,Ann MTH103A
McCarthy,Owen ART103A
McCarthy,Owen MTH103C
Rivera,Jane CSC201A
Rivera,Jane MTH101B
我希望这样每个名字只出现在第一列一次。任何帮助或想法都非常感激。
答案 0 :(得分:0)
由于行彼此独立,因此没有行可以知道它是否是第一行。
你可以做的是GROUP BY Name并将所有classNumbers放在一个像这样的列表中:
SELECT lastName + ',' + firstName AS Name,
GROUP_CONCAT(classNumber SEPARATOR '\n') AS classNumbers
FROM student
RIGHT JOIN Enroll ON (Student.stuid = Enroll.stuid)
GROUP BY Name
答案 1 :(得分:0)
如果只出现一个班级。
SELECT lastName + ',' + firstName as Name, classNumber
FROM STUDENT
RIGHT JOIN
(select Student.stuid as stuid, min(classNumber) as ClassNumber
from ENROLL Enroll inner Join Student
ON (Student.stuid = Enroll.stuid) group by Student.stuid) AS NewEnrol
ON NewEnroll.stuid = Student.stuid
或者
SELECT lastName + ',' + firstName as Name,Min(classNumber)
FROM STUDENT
RIGHT JOIN ENROLL ON Enroll.stuid = Student.stuid
Group By lastname+','+firstname