编写一个SQL语句,显示每个教师的姓名,以及他/她的报告官的姓名

时间:2017-02-19 10:00:29

标签: mysql

我在做第3个问题时遇到了麻烦,我想我应该使用内连接但我无法正确使用它。需要一些帮助。这是sql。

CREATE TABLE TeacherInfomation
(
TeacherName nvarchar(100),
TeacherID nvarchar(10),
ReportingOfficerID nvarchar(10),
primary key(TeacherName,TeacherID)
);

INSERT INTO TeacherInfomation VALUES ('Queenie','213P','204P')
INSERT INTO TeacherInfomation VALUES ('Rachel','225P','204P')
INSERT INTO TeacherInfomation VALUES ('Sarah','278P','254P')
INSERT INTO TeacherInfomation VALUES ('Tina','290P','245P')
INSERT INTO TeacherInfomation VALUES ('Violet','300T','213P')
INSERT INTO TeacherInfomation VALUES ('Wesley','301T','225P')
INSERT INTO TeacherInfomation VALUES ('Xena','204P','200P')
INSERT INTO TeacherInfomation VALUES ('Yvonne','245P','200P')
INSERT INTO TeacherInfomation VALUES ('Zachary','200P','NULL')

1) SELECT * FROM TeacherInfomation

2) SELECT TeacherName FROM TeacherInfomation WHERE TeacherID LIKE '%P'

Question im stuck at 3) SELECT COUNT(TeacherName) AS TotalNumberOfTeachers,ReportingOfficerID FROM TeacherInfomation GROUP BY ReportingOfficerID

QuestionPaper

2 个答案:

答案 0 :(得分:0)

问题是向所有教师展示他们的报告人员。因此,我们不需要任何countgroup by

这是我试过的:

select ti.TeacherName ri.ReportingOfficerName
from TeacherInformation ti
left join TeacherInfomation ri on ri.TeacherId = ti.ReportingOfficerId

我们还需要将自联接添加为ReportingOfficer,因为teacherId和reportingOfficierId是匹配的。

答案 1 :(得分:0)

你的查询sholud就像这样

      select 
      p1.TeacherName,p1.TeacherID,p1.ReportingOfficerID,p2.TeacherName 
      from TeacherInfomation p1 
      join TeacherInfomation p2 on p1.TeacherID = p2.ReportingOfficerID