创建视图 + 查询(组合列 + 添加额外属性)
Posted
技术标签:
【中文标题】创建视图 + 查询(组合列 + 添加额外属性)【英文标题】:Creating View + Query (Combining columns + adding an extra attribute) 【发布时间】:2012-11-24 21:23:09 【问题描述】:我设法在两个查询之间使用 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);
【问题讨论】:
当我将您的create table
s 和create view
输入SQL Fiddle 时,没有任何投诉。
表格应该可以工作,但“创建视图”对我不起作用。但我还没有尝试过使用“SQL Fiddle”。但是如果你让视图工作,你知道如何在视图中添加一个额外的列“状态”:是“注册”还是“等待”,这取决于我们是从表“RegisterdAt”还是“候补名单”
【参考方案1】:
您发布的工作解决方案看起来很棒。我只是将普通的 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
);
【讨论】:
谢谢你,甚至不知道有一个“UNION ALL”:)【参考方案2】:根据 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 进行进一步测试。
我删除了外键约束,因为这里没有定义各种表。
【讨论】:
没有设法用这种方式解决它,但我采取了不同的方法并让它工作,谢谢你的帮助。 @Pro9 你的解决方案怎么样?以上是关于创建视图 + 查询(组合列 + 添加额外属性)的主要内容,如果未能解决你的问题,请参考以下文章