3

I have tried joining 3 tables(class_section, section_name, student_detail) using below query based on two selected variables:

 SELECT A.cs_id
      , A.class
      , B.sec_id
      , B.cs_id
      , B.cs_name
      , C.stud_id
      , C.stud_class
      , C.stud_section
      , C.stud_adm_no
      , C.stud_full_name 
   FROM class_section AS A 
   JOIN section_name as B 
     ON A.cs_id = b.cs_id 
   JOIN student_detail as c 
     ON C.stud_class = B.cs_id 
  WHERE A.cs_id = $cat 
    AND B.sec_id = $subcat

And now, how can I join 4 tables, 3 tables as above and 4th table is exam_schedule(ex_schedule_class,ex_schedule_section,ex_stud_id,ex_schedule_name) (where(ex_schedule_class(as relationship with cs_id in class_section ), ex_schedule_section(as relationship with sec_id in section_name) and ex_stud_id(as relationship with stud_id in student_detail) from exam_schedule table I want to retrieve the ex_schedule_name value. Please help me.

vijay
  • 31
  • 4
  • We need to know which columns that can be used as glue. What column from exam_schedule to which other table, with what column? Also please read this: http://www.w3schools.com/sql/sql_join.asp – Blaatpraat Jun 22 '15 at 07:45
  • @Blaatpraat it's probably just professional rivalry, but that resource is discouraged over here. – Strawberry Jun 22 '15 at 07:59
  • First you need to decide whether you need an INNER or OUTER join, if you need an OUTER join, you'll then need to decide whether you'll need a LEFT or RIGHT OUTER join. – bashaus Jun 22 '15 at 08:02
  • @Strawberry: didn't know about that rivalry, just trying to help the OP. I can write the code he want, but it is better that he just know how that this part works. Another resource I've found that should be allowed :-) : http://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins – Blaatpraat Jun 22 '15 at 08:02
  • In case that your database is case sensitive, you should correct your different aliases. ie : declare "sudent_detail" alias as "C" (instead of "c"). This may not be the solution but it can help – rafatic Jun 22 '15 at 08:05

1 Answers1

0

You can try the following

 SELECT A.cs_id
  , A.class
  , B.sec_id
  , B.cs_id
  , B.cs_name
  , C.stud_id
  , C.stud_class
  , C.stud_section
  , C.stud_adm_no
  , C.stud_full_name 
  , D.name
   FROM class_section AS A 
   JOIN section_name AS B ON A.cs_id = b.cs_id 
   JOIN student_detail AS c ON C.stud_class = B.cs_id 
   JOIN exam_schedule AS D ON D._class = A.cs_id
   WHERE A.cs_id = $cat AND B.sec_id = $subcat

By joining "exam_shedule" with "class_section" you can get exam_schedule_name (in the query : D.name)

rafatic
  • 223
  • 1
  • 10
  • I have tried as per ur suggestion it is repeating for two times as "SELECT A.cs_id , A.class , B.sec_id , B.cs_id , B.cs_name , C.stud_id , C.stud_class , C.stud_section , C.stud_adm_no , C.stud_full_name , D.ex_schedule_name FROM class_section AS A JOIN section_name AS B ON A.cs_id = b.cs_id JOIN student_detail AS c ON C.stud_class = B.cs_id JOIN exam_schedule AS D ON D.ex_schedule_class = A.cs_id WHERE A.cs_id = 1 AND B.sec_id = 1" – vijay Jun 22 '15 at 09:28
  • I don't get what is repeating, Your program is returning the query as a string ? – rafatic Jun 22 '15 at 09:45