MySQL查询问题

时间:2010-08-06 17:32:57

标签: mysql

到目前为止,我可以列出所有学生的论文,但我希望能够只显示评分的学生论文,并想知道我将如何做到这一点?我还需要添加什么来添加我的MySQL代码?

提前感谢您的帮助!

这是我到目前为止用MySQL代码获得的内容。

SELECT students.*, students_essays.* 
FROM students 
INNER JOIN students_essays ON students.student_id = students_essays.student_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)
);

2 个答案:

答案 0 :(得分:1)

只需内部联接到essays_grades表(如果你想要成绩,可以使用成绩表)。

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 

这里有一些提示......您可以为表名设置别名(通过在FROM子句中的表名后面提供别名)并停止用它们的全名引用它们,你可以只说JOIN并暗示它是一个内心的联系。

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

答案 1 :(得分:0)

只需加入论文表中的成绩表并检查现有记录。

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