tags:

views:

41

answers:

3

I'm trying to count all the graded essays but if an essay has been graded twice or more for some reason the code will count the same essay as many times it has been graded is there a way I can make sure that the essay is counted only once?

Thanks for the help in advance!

Here is the code.

SELECT COUNT(students_essays.id) 
FROM students_essays 
INNER JOIN essays_grades ON students_essays.id = essays_grades.students_essays_id 

Here is my MySQL code.

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)
);

THANKS everyone for the help I figured out the problem It wasn't even this query doh!

+2  A: 

SELECT COUNT(DISTINCT students_essays.id)

Borealid
DID not work :(
labs
@labs: Problem on your end. You must have two different rows with the same students_essays.id in them.
Borealid
Is there a way I can combine them?
labs
@labs: By having a proper database schema? If your IDs are not unique, how do you know which essays are "the same"? Is it the content? If so, use `COUNT(DISTINCT content))`. But really, consider normalizing your database. Note that with the schema you showed it's impossible for this query not to "work" in the way you specified in the OP.
Borealid
A: 

Use a LEFT JOIN insted of INNER JOIN

EDIT: Meaning, SELECT essays, so you only have 1, and LEFT JOIN that to marks

ari_aaron
? what do you mean forgive my ignorance.
labs
Try this:SELECT COUNT(students_essays.id) FROM students_essays LEFT JOIN essays_grades ON students_essays.id = essays_grades.students_essays_id
ari_aaron
@ari_aaron sadly no success :(
labs
Make sure the essays aren't in twice in the essays table.
ari_aaron
You can do DISTINCT(content) on the student_essays part if you want...
ari_aaron
A: 

Add DISTINCT to your query. Or, if you only need the id, don't bother with the students_essays table at all since the id is in essays_grades:

select count(distinct students_essays_id) from essays_grades
Scott Saunders
DID not work :(
labs
How did it not work? What did it do?
Scott Saunders
Still counted the duplicates.
labs
You ran the query I typed in above? It will not count duplicates unless 'duplicates' have different ids.
Scott Saunders
Yes I ran it. I'll run it again to make sure
labs
No success at all :(
labs
I think your data is bad.
Scott Saunders
how is my data bad?
labs
My understanding of your data structure is that essays_grades has a foreign key to a grade and a foreign key to an essay. So every time an essay is graded, there will be a row added to essays_grades with the id of the essay and the id of the grade record. My query counts the distinct essay ids in the essays_grades table. It will tell you exactly how many essays have had grades recorded. Either you didn't count right when determining what the query should return, or you don't have the proper rows in the essays_grades table.
Scott Saunders