두 번의 쿼리 사이에서 UNION을 사용하여 해결할 수있었습니다. 시도가 조금 있었지만 수학적으로 추가하려고했습니다. 이것은 당신이 그것을 할 수있는 최선의 방법이 아니라는 것을 눈치 채지 만, 효과가 있으며 저에게는 충분합니다. 도와 줘서 고마워.View + Query 만들기 (열 결합 + 추가 속성 추가)
근무 솔루션 :
CREATE VIEW Registrations AS
(SELECT S.identificationnumber AS StudentId, S.name AS StudentName, C.code AS CourseCode, C.name AS CourseName, 'Waiting' AS Status
FROM Waitinglist W, Student S, Course C
WHERE S.identificationnumber = W.identificationnumber
AND W.code = C.code) UNION (SELECT S.identificationnumber AS StudentId, S.name AS StudentName, C.code AS CourseCode, C.name AS CourseName, 'Registered' AS Status
FROM Registeredat R, Student S, Course C
WHERE S.identificationnumber = R.identificationnumber
AND R.code = C.code);
Origianl 문제 : 일이 그 전문을 볼되지 않을 수도 있습니다
나는, 데이터베이스 및 SQL에서 begginner입니다.
일반 텍스트로 수행하려는 작업 : 모든 과정의 모든 등록 된 대기중인 학생에 대한보기를 만들려고합니다. 또한 "등록"또는 "대기 중"이라는 새로운 "열"을 추가하려고합니다.
어떻게보기가보고 싶어 :
StudentID, StudentName, CourseCode, CourseName, Status
StudentID = Combined idenficationnumber for Table "RegisterdAt" and "Waitinglist"
StudentName = Based on StudentID find matching name in Table "Student"
CourseCode = Combined code for Table "RegisterdAt" and "Waitinglist"
CourseName = based on code find matching name in Table "Course"
Status = Either "registered" or "waiting"
depending on if we got the "row" from Table "RegisterdAt" or "Waitinglist"
생성 된 테이블 (나는 또한 쉽게 테스트를 위해, 그들로 일부 examplery 데이터를 추가 한) :
CREATE TABLE Student(
identificationnumber VARCHAR(20),
name VARCHAR(50),
branchname VARCHAR(50),
programmename VARCHAR(50),
PRIMARY KEY(identificationnumber),
FOREIGN KEY(branchname, programmename) REFERENCES Branch(name, programmename)
);
CREATE TABLE Course(
code CHAR(6),
name VARCHAR(50),
credits VARCHAR(10),
departmentname VARCHAR(50),
PRIMARY KEY(code),
FOREIGN KEY(departmentname) REFERENCES Department(name)
);
CREATE TABLE Waitinglist(
identificationnumber VARCHAR(20),
code CHAR(6),
ddate VARCHAR(10),
PRIMARY KEY(identificationnumber, code),
FOREIGN KEY(identificationnumber) REFERENCES Student(identificationnumber),
FOREIGN KEY(code) REFERENCES Course_with_maxstudents(code)
);
CREATE TABLE Registeredat(
identificationnumber VARCHAR(20),
code CHAR(6),
PRIMARY KEY(identificationnumber,code),
FOREIGN KEY(identificationnumber) REFERENCES Student(identificationnumber),
FOREIGN KEY(code) REFERENCES Course(code)
);
만드는 시도 뷰가 (하지 작업 및 등록을 한 누락/대기 속성) :
CREATE VIEW Registrations AS
SELECT (R.identificationnumber + W.identificationnumber) AS StudentId, S.name AS StudentName, (R.code + W.code) AS CourseCode, C.name as CourseName
FROM Registeredat R, Waitinglist W, Student S, Course C
WHERE S.identificationnumber = (R.identificationnumber + W.identificationnumber)
AND C.code = (R.code + W.code);
난 당신'table's를 만들고'[SQL 바이올린 (http://www.sqlfiddle.com/#!2/4c307에 view'을 만들 입력하면), 불만은 없습니다. –
테이블이 작동해야합니다, "보기 만들기"는 나를 위해 일하지 않을 것입니다. 하지만 "SQL Fiddle"을 사용하려하지 않았습니다. 그러나보기가 작동하면보기에 "Status"라는 추가 열을 추가하는 방법을 알고 있습니까? "RegisterdAt"테이블에서 "row"를 얻었는지 아니면 " Waitinglist " – Pro9