Hi all,
I don't seem to be getting the results I'm looking for with the query below:
Code:
SELECT scores.test_id, score, question_id
FROM scores
LEFT JOIN wrongs USING (member_id, subject, test_id)
WHERE scores.member_id='1'
AND scores.subject='English'
ORDER BY scores.test_id DESC LIMIT 3
The two tables being used store the following info:
Code:
scores
member_id subject test_id score date
1 English 1 80 2004-01-17
1 English 2 40 2004-01-17
1 English 3 80 2004-01-17
1 English 4 80 2004-01-19
1 English 5 80 2004-01-19
1 English 6 100 2004-01-19
1 English 7 60 2004-01-19
wrongs
member_id subject test_id question_id
1 English 1 2
1 English 2 8
1 English 2 9
1 English 2 10
1 English 3 12
1 English 4 20
1 English 5 25
1 English 7 34
1 English 7 35
The output I got from that query is as follows:
Code:
test_id score question_id
7 60 34
7 60 35
6 100 NULL
I was expecting something like:
Code:
test_id score question_id
7 60 34
7 60 35
6 100 NULL
5 80 25
What I'm trying to do is selecting the last 3 tests from the table 'scores' with their corresponding wrong question numbers in the table 'wrongs'. How would the query be like?
Could someone help to enlighten me?
Updated:
The following gets the results I need:
Code:
SELECT scores.test_id, score, question_id
FROM scores
LEFT JOIN wrongs USING (member_id, subject, test_id)
WHERE scores.member_id='1'
AND scores.subject='English'
AND scores.test_id > 4
ORDER BY scores.test_id DESC
But is there a better way of writing the query?