If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with query...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-19-04, 03:04
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Help with query...

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?

Last edited by pearl2; 01-19-04 at 03:21.
Reply With Quote
  #2 (permalink)  
Old 01-19-04, 09:14
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
if you are not on mysql 4.1, write two queries, the first to get the last 3 tests and the second to get the scores, using the ids from the first query in the WHERE clause with id IN (id1, id2, id3)

if you are on 4.1, this can be done in one query with id IN (subquery)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-20-04, 02:11
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Thanks, r937!

I'm using MySQL 4.0.16. I'm not sure if sub-query is supported in that version but I'll experiment with it later...

I need to progressively select the tests in descending order (as in click a link to select first 10, then select the next 10, and so on). The following seems to work:
Code:
# using perl../
$lower_bound = some_id;
$upperbound = some_id;
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 > $lower_bound AND scores.test_id <= $upperbound
   };
That code selects scores and wrong question IDs from tests that are above a certain id and up to the id defined by $upperbound.
Reply With Quote
  #4 (permalink)  
Old 01-20-04, 02:38
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
4.0.16 is less than 4.1 so you cannot use subqueries

setting upper and lower bounds works just as nicely as listing the test numbers in an IN list

now all you have to do is get the test numbers

you said initially "the last 3 tests from the table 'scores'" and now you want to page through them, well, all you have to do is utilize the LIMIT keyword properly

select test_id
from scores
where member_id='1'
and subject='English
limit 0,10

the first time you run it, use LIMIT 0,10

the second time you run it, use LIMIT 10,10

then LIMIT 20,10 and so on

after each time you run it, use the lowest and highest test_id numbers that were returned by that query to run your LEFT OUTER query
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-02-04, 08:45
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Thanks, r937!

Sorry I haven't been online for a while and didn't read your reply till today.

I managed to get the desired results using lower and upper bounds. But I now have another option given in your solution using LIMIT. Will experiment with that.

Cheers and thanks once again

pearl2


Quote:
Originally posted by r937
4.0.16 is less than 4.1 so you cannot use subqueries

setting upper and lower bounds works just as nicely as listing the test numbers in an IN list

now all you have to do is get the test numbers

you said initially "the last 3 tests from the table 'scores'" and now you want to page through them, well, all you have to do is utilize the LIMIT keyword properly

select test_id
from scores
where member_id='1'
and subject='English
limit 0,10

the first time you run it, use LIMIT 0,10

the second time you run it, use LIMIT 10,10

then LIMIT 20,10 and so on

after each time you run it, use the lowest and highest test_id numbers that were returned by that query to run your LEFT OUTER query
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On