CREATE TABLE student
(
stuID INT IDENTITY(1,1),
stuFName VARCHAR(30) NOT NULL,
stuLName VARCHAR(30) NOT NULL,
stuEmail VARCHAR(100) NOT NULL
CONSTRAINT pk_student_stuID PRIMARY KEY (stuID)
);
INSERT INTO student (stuFName, stuLName, stuEmail)
VALUES
('Billy-Bob', 'MacBeth', 'dragonSlayer@gmail.com'),
('Rob-Roy', 'MacGregor', 'rMacGregor1@yahoo.com'),
('Sweeny', 'MacSituation', 'EyeSwearInGaelic@yahoo.com'),
('Kim', 'MacDassian', 'kayDee@gmail.com'),
('Monte', 'MacHaggis', 'meanMonty01@yahoo.com');
CREATE TABLE course
(
courseCode CHAR(6),
courseName VARCHAR(50) NOT NULL
CONSTRAINT pk_course_courseCode PRIMARY KEY (courseCode)
);
INSERT INTO course (courseCode,courseName) VALUES
('PHY200', 'Quantum Physics'),
('MAT100', 'Statistics'),
('BIO250', 'Biomechanics'),
('SOC100', 'Intro to Sociology'),
('PSY101', 'Human Perception');
CREATE TABLE tutor_session
(
tsID INT IDENTITY(1,1),
tsMeetingDateTime DATETIME2 NOT NULL,
tsLength TINYINT NOT NULL,
tsMeetingConfirmed BIT DEFAULT 0,
learnerID INT NOT NULL,
tutorID INT NOT NULL,
courseCode CHAR(6) NOT NULL,
CONSTRAINT pk_tutorSession_tsID PRIMARY KEY(tsID),
CONSTRAINT fk_learner_tutorSession FOREIGN KEY(learnerID) REFERENCES student(stuID),
CONSTRAINT fk_tutor_tutorSession FOREIGN KEY(tutorID) REFERENCES student(stuID),
CONSTRAINT fk_course_tutorSession FOREIGN KEY(courseCode) REFERENCES course(courseCode)
);
INSERT INTO tutor_session (tsMeetingDateTime, tsLength, learnerID, tutorID, courseCode) VALUES
('2015-11-01 08:00AM', 2, 1, 2, 'PHY200'),
('2015-11-01 10:00AM', 1, 3, 2, 'MAT100'),
('2015-11-01 08:00AM', 2, 1, 2, 'PHY200'),
('2015-11-02 03:00PM', 1, 2, 1, 'BIO250'),
('2015-11-03 02:00AM', 3, 1, 2, 'SOC100'),
('2015-11-04 02:00AM', 3, 1, 2, 'SOC100'),
('2015-11-10 02:00AM', 1, 4, 2, 'PSY101'),
('2015-11-03 02:00AM', 1, 5, 2, 'PSY101');
这将在前两列中输入相同的名称,即“Learner”和#39;名称。我想做的是与“导师”联系。别名是它的合适价值。
I'm trying to get an input identical to this one
SELECT
tsID, stuFName + ' ' + stuLName AS 'Tutor',
stuFName + ' ' + stuLName AS 'Learner',
courseName, tsMeetingDateTime
FROM
tutor_session
JOIN
student ON tutor_session.learnerID = student.stuID
JOIN
course ON tutor_session.courseCode = course.courseCode
ORDER BY
courseName asc
答案 0 :(得分:0)
@MarcB说的是什么。只需加入学生表两次..一次用于学习者,再一次用于导师..在你的联接上使用别名来知道哪个是哪个。
SELECT
tsID,
tutor.stuFName + ' ' + tutor.stuLName AS 'Tutor',
learner.stuFName + ' ' + learner.stuLName AS 'Learner',
courseName,
tsMeetingDateTime
FROM
tutor_session
JOIN
student as learner ON tutor_session.learnerID = learner.stuID
JOIN
student as tutor ON tutor_session.tutorID = tutor.stuID
JOIN
course ON tutor_session.courseCode = course.courseCode
ORDER BY
courseName ASC