0

The following select statement returns a list of students:

SELECT * FROM students2014 ORDER BY LastName

However, for each row I need to return data from another table (notes2014) to allow me to display the latest note for each student. The select statement would be as follows:

SELECT Note FROM notes2014 WHERE NoteStudent='$Student'

$Student indicates the ID for each student in the students2014 database. However this result only appears after the initial statement is queried.

So my question is, how can I run the second query within the first?

1
  • You probably need a join. Commented Oct 23, 2013 at 9:24

3 Answers 3

1

Posted on behalf of the OP:

I've worked it out! Thank you for you help. I used left join in the end:

SELECT * FROM students2014
    LEFT JOIN notes2014 ON students2014.Student = notes2014.NoteStudent
    WHERE students2014.Consultant='$Consultant'
    ORDER BY students2014.LastName
Sign up to request clarification or add additional context in comments.

Comments

0

you should query the data like this:

select students2014.*, notes2014.note from students2014, notes2014 where students2014.id = notes2014.NoteStudent

1 Comment

That works! Thank you! However the results exclude any students that don't presently have any corresponding notes... I need to display a complete list even if some students don't currently have a note.
0

Try this::

Select 
Student, 
Note 
from 
students2014 s
INNER JOIN notes2014 n on Student = n.NoteStudent
ORDER BY lastName

3 Comments

I'm afraid I'm totally lost with this. I've tried it and I'm getting absolutely nothing. What do you mean by studentId and student_ID? The column that indicates student ID is Student.
@PhilHowell: what is the studentId column in student2014?
It's called Student :) It's just an incremental value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.