2012-11-24 53 views
2

我设法解决它在两个查询之间使用UNION,我相信我的尝试是有点关闭,并试图做一个数学增加。这是problaby不是你可以做到的最好方式,但它有效,对我来说就足够了。感谢您的帮助。创建视图+查询(组合列+添加额外的属性)

工作液:

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); 

原著的问题:

我在数据库和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和'创建view'到[SQL小提琴(http://www.sqlfiddle.com/#!2/4c307 ),没有投诉。 –

+0

表应该工作,“创建视图”不会为我工作,虽然。但我没有尝试过使用“SQL小提琴”。但是如果你看到这个视图可以工作,你知道如何在视图中添加一个额外的列“状态”:根据我们是从表“RegisterdAt”还是“ Waitinglist“ – Pro9

回答

2

您发布的工作解决方案看起来不错。我只是把普通的UNION变成UNION ALL,因为你似乎不太可能需要删除这两个子查询之间的重复。 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

谢谢,甚至不知道有一个“联盟所有”:) – Pro9

0

添加列状态“注册”,这取决于r.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, 
     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进行进一步的测试。

我放弃了外键约束,因为这里没有定义各种表。

+0

没有设法解决它使用这种方式,但我采取了不同的方法,并得到它的工作,感谢您的帮助。 – Pro9

+0

@ Pro9你的解决方案看起来如何? –