sql - Mysql Help - Left join -
i read other threads , first question here. here goes;
i'm trying build query involves 2 tables, course table , studentslink table. studentslink table describes link between students , course. tables below;
course courseid(bigint) - pk coursename (varchar) courseinstructor (varchar) studentslink courseid(bigint) - pk studentid(bigint) - pk below sample data; course table id | coursename| courseinstructor ---------------------------------- 1 | algebra 1 | mike 2 | english 2 | james 3 | english 3 | john 4 | algebra 2 | mike 5 | history 1 | tony studentlink table studentid | courseid ---------------------- 100 | 2 101 | 3 102 | 3 102 | 4 103 | 4 100 | 1 103 | 3 103 | 2 the desired outcome below given if looking student number 103
id | coursename| courseinstructor |studentid | courseid --------------------------------------------------------- 1 | algebra 1 | mike | null | null 2 | english 2 | james | 103 | 2 3 | english 3 | john | 103 | 3 4 | algebra 2 | mike | 103 | 4 5 | history 1 | tony | null | null the query have far below;
select * course left join studentlink on course.courseid = studentlink.courseid studentlink.studentid = 103 or (studentlink .studentid null , studentlink.courseid null) order studentlink.courseid desc i'm trying result set of out courses available, 1 particular student registered in , 1 not able display course can offer student.
i have tried many variations of query , did research. i'm not asking teh codez little bit of guidance wonderful. i've been stuck @ few days while trying work other parts of project @ same time.
any appreciated. in advance.
select id, coursename, courseinstructor, studentid, courseid courses c left join studentlink sl on c.id = sl.courseid , studentid = 103
Comments
Post a Comment