SQL:用于评估的内部联接帮助

时间:2016-01-20 10:58:18

标签: mysql sql inner-join

在学校的受控评估中,我坚持这个问题:

创建,运行,测试,解释和演示脚本以执行以下操作:

  1. 使用OCR考试板生成所有条目的列表,显示:
    • 有条目的学生的姓名
    • 为学生输入的考试的科目名称和录取水平。
  2. 制作所有学生的名单,显示学生的姓名,然后是考试 拍摄。此列表应按学生姓氏的字母顺序显示。
  3. 在我的代码中,我不知道如何使用INNER JOIN加入2个以上的表,但如果我尝试'ON'语句不想工作,我不知道如何解决这个问题

    CREATE个表格和INSERT数据:

    CREATE TABLE IF NOT EXISTS students
    (
    student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    first_name VARCHAR(20) NOT NULL,
    middle_name VARCHAR(20),
    last_name VARCHAR(40) NOT NULL,
    email VARCHAR(60) NOT NULL,
    password CHAR(40) NOT NULL,
    reg_date DATETIME NOT NULL,
    PRIMARY KEY (student_id),
    UNIQUE (email)
    );
    
    INSERT INTO students (first_name,last_name,email,password,reg_date) VALUES 
    ("ex1","ex1.1","example1@gmail.com","11062001",'2009-12-04 13:25:30'),
    ("ex2","ex2.2","example2@gmail.com","ex123",'2015-02-12 15:20:45'),
    ("my name is jeff","21","kid","mynameis21kid@vine.com","yolo",'2014-09-21 14:15:25'),
    ("Mr.Right","Mr.Calvin","Mr.Hildfiger","Mr.misters@mister.com","mistermaster",'2015-06-04 19:50:35'),
    ("Bob","Dabuilda","bobthebuilder@fixit.com","BTBCWFI?",'2005-11-12 21:20:55');
    
    CREATE TABLE IF NOT EXISTS subjects
    (
    subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    subject_name VARCHAR(20) NOT NULL,
    level_of_entry VARCHAR(5) NOT NULL,
    exam_board VARCHAR(10) NOT NULL,
    PRIMARY KEY (subject_id),
    UNIQUE(subject_id)
    );
    
    INSERT INTO subjects (subject_name,level_of_entry,exam_board) VALUES 
    ("Chemistry","AS","OCR"),
    ("Biology","GCSE","AQA"),
    ("Music","GCSE","Edexcel"),
    ("English","A","OCR"),
    ("Physics","A","AQA"),
    ("Computing","GCSE","Edexcel"),
    ("French","A","AQA"),
    ("Maths","AS","OCR"),
    ("Product Design","GCSE","AQA"),
    ("History","AS","OCR");
    
    CREATE TABLE IF NOT EXISTS entries
    (
    entry_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    date_of_exam DATE NOT NULL,
    student_id INT UNSIGNED NOT NULL,
    subject_id INT UNSIGNED NOT NULL,
    FOREIGN KEY (student_id) REFERENCES students(student_id),
    FOREIGN KEY (subject_id) REFERENCES subjects(subject_id),
    PRIMARY KEY (entry_id)
    );
    
    INSERT INTO entries (date_of_exam, student_id, subject_id) VALUES 
    ('2015-05-31', 1, 6),
    ('2015-05-31', 2, 10),
    ('2015-01-21', 3, 3),
    ('2015-01-21', 4, 7),
    ('2015-09-13', 5, 1),
    ('2015-09-13', 2, 9),
    ('2015-12-06', 4, 8),
    ('2015-12-06', 1, 2),
    ('2015-04-01', 3, 5),
    ('2015-04-01', 5, 4);
    

    SELECT

    SELECT entries.*, subjects.subject_name, subjects.level_of_entry
    FROM subjects
        INNER JOIN entries,
                   students ON entries.subject_id = subjects.subject_id
    WHERE subjects.exam_board LIKE "OCR%";
    

3 个答案:

答案 0 :(得分:0)

使用明确的JOIN,现在就开始,永远不要回头!还要使用别名来保持整洁。

SELECT e.*, su.subject_name, su.level_of_entry
FROM subjects su
INNER JOIN entries e ON e.subject_id = su.subject_id
INNER JOIN students st ON e.student_id = st.student_id 
WHERE su.exam_board LIKE "OCR%";

答案 1 :(得分:0)

在连接后立即提供连接条件,以使其简单易读。正如jarlh所写,不要将显式连接与逗号分隔列表混合。如果您知道要查找的确切值,则无需使用模式匹配。只需使用具有确切值的=运算符。

SELECT *
FROM subjects
INNER JOIN entries ON entries.subject_id = subjects.subject_id --join 2 tables
INNER JOIN students ON entries.student_id=students.student_id --join the 3rd tables
WHERE subjects.exam_board = "OCR";

答案 2 :(得分:0)

我认为您可能需要看起来像这样的代码:

select entries.*, subjects.subject_name, subjects.level_of_entry
from entries
join students on students.student_id = entries.student_id
join subjects on subjects.subject_id = entries.subject_id
where exam_board = 'OCR';

我希望这会有所帮助。