3

2 つのクエリ間で 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 にするだけです。これら 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  
);  
于 2012-11-25T16:52:54.777 に答える
0

r.codeがnullでない場合は「登録済み」、それ以外の場合は「待機中」の列ステータスを追加します

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フィドル。

さまざまなテーブルがここで定義されていないため、外部キー制約を削除しました。

于 2012-11-24T22:36:29.857 に答える