MySQL查询问题

时间:2010-08-06 21:25:32

标签: mysql

到目前为止,我可以列出所有学生的论文和评分的学生论文,并想知道如何才能展示所有未评级的论文?我还需要添加什么来添加我的MySQL代码?

提前感谢您的帮助!

这是我到目前为止的代码。

SELECT students.*, students_essays.*  
FROM students  
INNER JOIN students_essays ON students.student_id = students_essays.student_id 
INNER JOIN essays_grades ON students_essays.id = essays_grades.students_essays_id
ORDER BY students_essays.id DESC 

这是我的MySQL表格。

CREATE TABLE students_essays (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
student_id INT UNSIGNED NOT NULL,
content TEXT NOT NULL,
PRIMARY KEY (id)
);


CREATE TABLE students (
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
student_first_name VARCHAR(255) DEFAULT NULL,
student_last_name VARCHAR(255) DEFAULT NULL,
pass CHAR(40) NOT NULL,
PRIMARY KEY (student_id)
);


CREATE TABLE essays_grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
grade_id INT UNSIGNED NOT NULL,
students_essays_id INT UNSIGNED NOT NULL,
student_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
letter_grade VARCHAR(2) DEFAULT NULL,
grade_points FLOAT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);

3 个答案:

答案 0 :(得分:1)

SELECT students.*, students_essays.*  
FROM students  
INNER JOIN students_essays ON students.student_id = students_essays.student_id 
INNER JOIN essays_grades ON students_essays.id = essays_grades.students_essays_id
INNER JOIN grades ON essays_grades.grade_id = grades.id
WHERE LETTER_GRADE IS NULL
ORDER BY students_essays.id DESC;

我使用了INNER JOIN,就像你一样。请注意,如果找不到右表中的匹配条目,则内部联接将隐藏左表中的行。如果您想要包含左连接,请使用左连接。

如果论文未叙述,我认为letter_grade为null。如果您的数据库没有essays_grades行,如果文章没有评分,查询将更简单:

SELECT students.*, students_essays.*  
FROM students  
INNER JOIN students_essays ON students.student_id = students_essays.student_id 
LEFT JOIN essays_grades ON students_essays.id = essays_grades.students_essays_id
WHERE essays_grades.grade_id IS NULL
ORDER BY students_essays.id DESC;

答案 1 :(得分:0)

SELECT students.*, students_essays.*   
FROM students   
INNER JOIN students_essays ON students.student_id = students_essays.student_id  
LEFT JOIN essays_grades ON students_essays.id = essays_grades.students_essays_id 
WHERE essays_grades.students_essays_id is null
ORDER BY students_essays.id DESC  

答案 2 :(得分:0)

未评级为letter_grade,e。 G。 'UG'?

在这种情况下,请添加

WHERE letter_grade = 'UG'
ORDER BY之前