2012-11-24 2 views
2

두 번의 쿼리 사이에서 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); 
+0

난 당신'table's를 만들고'[SQL 바이올린 (http://www.sqlfiddle.com/#!2/4c307에 view'을 만들 입력하면), 불만은 없습니다. –

+0

테이블이 작동해야합니다, "보기 만들기"는 나를 위해 일하지 않을 것입니다. 하지만 "SQL Fiddle"을 사용하려하지 않았습니다. 그러나보기가 작동하면보기에 "Status"라는 추가 열을 추가하는 방법을 알고 있습니까? "RegisterdAt"테이블에서 "row"를 얻었는지 아니면 " Waitinglist " – Pro9

답변

2

게시 한 작업 솔루션이 훌륭하게 보입니다. 나는 UNION ALL으로 일반 UNION을 만들 것이다. 왜냐하면이 2 개의 서브 쿼리 사이에서 중복을 제거 할 필요가 없을 것 같기 때문이다. ALL은 서버가 결합 된 결과를 사용하고 존재하지 않는 중복을 검색하기 위해 불필요한 작업을 수행하지 못하게합니다.

그래서이 될 것입니다 :

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 ALL 
( 
    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 
); 
+0

감사합니다. "UNION ALL"이 있다는 것을 알지 못했습니다. :) – Pro9

0

추가 열 상태는 그렇지 않으면 더 테스트를 위해

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, 
     case when r.code is not null then 'registered' else 'waiting' end as status 
FROM Registeredat R, Waitinglist W, Student S, Course C 
WHERE S.identificationnumber = (R.identificationnumber + W.identificationnumber) 
AND C.code = (R.code + W.code); 

SQL Fiddle "대기", r.code이 null 인에 따라 "등록".

다양한 테이블이 여기에 정의되어 있지 않으므로 외래 키 제약 조건이 삭제되었습니다.

+0

Didnt는이 방법으로 문제를 해결할 수 있었지만 다른 접근법을 취하여 작동 시켰습니다. 도움을 요청하십시오. – Pro9

+0

@ Pro9 그리고 당신의 해결책은 어떻게 생겼습니까? –