Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select problem

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-11-09 : 08:18:19
farid writes "i have 2 tables
t_students
student_id = 1
student_name = Monica

t_courses
course_id = 1
student_id = 1
course_name= word

course_id = 2
student_id = 1
course_name= excel



when i search for exapmle using like "like student name" i want to
display one record containing student_name and the last course like the following:

student name course name course_id
Monica excel 2


but i used max fuction i get


student name course name course_id
Monica word 2



beacuse the letter w is after letter e

Please help me and thank you for all.
Regards,

--------------------------------------------------------------------------------"

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-11-09 : 08:36:52
[code]
select s.student_name, c.course_name, c.course_id
from t_students s inner join t_courses c
on s.student_id = c.student_id
and c.course_id = (select max(course_id) from t_courses x where x.student_id = s.student_id)
[/code]


KH

Go to Top of Page
   

- Advertisement -