Jump to content

Sub Select or NOT IN really confused


jmaccs

Recommended Posts

Hello all,

 

I am really confused.... Here is what I have, I hope someone can help!

 

mysql 5.067

 

students table

|student_id|first_name|last_name|grade_level

 

idtrack table

id|timestamp|student_id|event_id

 

I want to select all the students that ARE NOT in the idtrack table, but in the students table, an exceptions list...essentially this:

 

|student_id|first_name|last_name|grade_level|event_id

 

...of those students with the defined grade_level and event_id

 

SELECT students.student_id, students.last_name, students.first_name, students.grade_level, idtrack.event_id
FROM students
LEFT JOIN idtrack ON students.student_id = idtrack.student_id
WHERE (students.grade_level = 11
|| idtrack.student_id IS NULL || idtrack.event_id=6)

 

every time I try the above query event_id field turns up NULL in the results.

Here is my EXPLAIN:

 

id select_type table type possible_keys key key_len ref rows Extra

1 SIMPLE students ALL NULL NULL NULL NULL 3775

1 SIMPLE idtrack ALL NULL NULL NULL NULL 6 Using where

 

Any help?

 

Thanks in advance!

Joe

 

 

Link to comment
https://forums.phpfreaks.com/topic/211849-sub-select-or-not-in-really-confused/
Share on other sites

try this

 

SELECT students.student_id, students.last_name, students.first_name, students.grade_level, idtrack.event_id
FROM students
LEFT JOIN idtrack ON students.student_id = idtrack.student_id
WHERE students.grade_level = 11
      AND ( idtrack.student_id IS NULL  OR idtrack.event_id=6)

 

this is getting all the student with grade_level 11 who, are not present in the idtrack table or those with event_id = 6

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.