2010-11-18 3 views
0

私は以下のMySQLテーブルを持っています。彼らは学校でCSコースを代表し、応募者は特定のコースのTA(指導助手)になる。MySQLクエリ経由でポジションの申請者を選ぶ

各コースの「ベスト」応募者を印刷するクエリを作成したいとします。最高の応募者の制約条件は、Applicants.level = 7の応募者が最初に照合されます。

  1. ApplicantsToCourses.returning = trueの応募者が2番目に選ばれます。
  2. 他のすべての応募者は、それ以上の差別を要することなく照合されます。

テーブル定義は以下のとおりです。クエリで

CREATE TABLE Courses (
course_number SMALLINT(3) UNSIGNED NOT NULL, 
course_section SMALLINT(1) UNSIGNED NOT NULL, 
name CHAR(30) NOT NULL, 
instructor CHAR(30), 
lab_time CHAR(30), 
PRIMARY KEY(course_number, section), 
FOREIGN KEY(course_number, section) REFERENCES ApplicantsToCourses(course_number, course_section) 
) 

CREATE TABLE Applicants (
student_id CHAR(10) NOT NULL, 
name CHAR(30), 
email CHAR(30), 
gpa DECIMAL(4,3) UNSIGNED, 
level CHAR(2), 
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 
PRIMARY KEY(student_id), 
FOREIGN KEY(student_id) REFERENCES ApplicantsToCourses(student_id), 
CHECK(gpa <= 4.000) 
) 

CREATE TABLE ApplicantsToCourses (
student_id CHAR(10) NOT NULL, 
returning BOOLEAN DEFAULT FALSE NOT NULL, 
course_number SMALLINT(3) UNSIGNED NOT NULL, 
course_section SMALLINT(1) UNSIGNED NOT NULL, 
PRIMARY KEY(student_id, course_number, course_section), 
FOREIGN KEY(student_id) REFERENCES Applicants(student_id), 
FOREIGN KEY(course_number, course_section) REFERENCES Courses(course_number, course_section) 
) 

私の試みでした。 。 。

select a.student_id, ac.course_number, ac.course_section 
from Applicants a, ApplicantsToCourses ac, Courses c 
where a.student_id = ac.student_id and ac.course_number = c.course_number and ac.course_section = c.course_section 
order by a.level, ac.returning desc 

。 。 。確かに正しいロジックを持っていません。

+0

これは宿題に関する質問ですか?どのようにクエリが間違っていますか?それが示す結果に何が間違っていますか? –

答えて

0

次の擬似コードを使用して、最終的な解決策に達するのに役立つ一時テーブルを作成できます。

SELECT * 
FROM Applicants APP 
JOIN ApplicantsToCourses ATC ON ATC.student_id = APP.student_id 
JOIN Courses COU ON COU.number = ATC.course_number AND COU.section = ATC.course_section 
WHERE APP.level = 7 

SELECT * 
FROM Applicants APP 
JOIN ApplicantsToCourses ATC ON ATC.student_id = APP.student_id 
JOIN Courses COU ON COU.number = ATC.course_number AND COU.section = ATC.course_section 
WHERE ATC.returning = true 
関連する問題