MySQL COUNT问题

时间:2010-08-06 23:03:53

标签: mysql

有没有办法可以计算出有多少未叙级的论文可以展示?如果是这样我还需要添加或删除我的MySQL代码?

提前感谢您的帮助!

这是我的MySQL代码。

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;

这是我的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 :(得分:2)

SELECT count(*)  
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 :(得分:2)

据我所知,你只需要查看2个表格。

SELECT COUNT(*) 
FROM students_essays se
WHERE NOT EXISTS(SELECT * FROM 
                 essays_grades ge
                 WHERE se.id = eg.students_essays_id)