3

我设法在两个查询之间使用 UNION 解决了​​它,我相信我的尝试有点偏离并尝试进行数学加法。这可能不是你能做到的最好方法,但它有效,这对我来说已经足够了。感谢您的帮助。

工作解决方案:

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 的初学者,所以事情可能看起来不那么专业。

我正在尝试以纯文本方式执行的操作: 我正在尝试为所有注册和等待的学生以及所有课程创建一个视图。我还想添加一个新的“列”,即“已注册”或“等待”。

我希望视图看起来如何:

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" 

创建的表(我还在其中添加了一些示例数据,以便于测试):

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

2 回答 2

3

您发布的工作解决方案看起来很棒。我只会将普通的 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  
);  
于 2012-11-25T16:52:54.777 回答
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用于进一步测试。

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

于 2012-11-24T22:36:29.857 回答