简体   繁体   中英

using multiple joins codeigniter

I'm working on an academic CMS where I have to display query result being pulled from 5 tables using multiple joins. I am not getting the required results. Here are the table schemas:

class

class_id    class_year  class_semester  class_course    class_status
1           2014         6                   5               1
2           2014         6                   3               1
3           2014         6                   1               1
4           2014         6                   2               1
5           2014         6                   6               1

Here, class_course is a foreign key in the course table as course_id

class_student

class_student_id    class_id    student_id  class_marks
       1                1          s-14-1        5
       2                2          s-14-1        2
       3                2          s-14-2        0

course

course_id   course_code course_name             course_credit
    1         MT-001    Calculus I                    3
    2         MT-002    Calculus II                   3
    3         CS-001    Computer Programming          4
    4         CS-002    Computer Fundamental          4
    5         MG-001    Fundamental of Management     3
    6         CS-098    Advance Programming           4

course_offer

co_id   co_semester course_code
  1          1         MT-001
  2          1         CS-002
  3          1         MG-001
  4          1         CS-001
  5          2         CS-098
  6          2         MT-002

Here, co_semester shows that this course is offered for students of which semester suppose 1, 2 3 4 etc.

course_prerequisite

cp_id   course_id   prereq_id
  1           6           3
  2           2           1

Here, course_id and prereq_id are foreign keys in the course table as course_id

Now, the desired query output required is based on these terms/conditions:

Show all classes which are active ( class_status=1 ) and...

  1. which do not have any pre-requisite course ( class_course to be compared with course_prerequisite table) and was never studied before ( class_student.class_id should not be available)
  2. which do not have pre-requisite course but was studied and failed, now the class is again scheduled to study
  3. which have prerequisite course but the prerequisite course has been passed by the student ( class_student.class_marks > 50 )

here is sample of desired output

year semester course_code course_name                   course_credit
2014    6         MG-001    Fundamental of Management        3
2014    6         CS-001    Computer Programming             4
2014    6         MT-001    Calculus I                       3

Here MG-001, CS-001 and MT-001 does not have prerequisite and was not studied before, so must be shown to student.

from class table

CS-0098 has a prerequisite class, CS-001 Computer Programming, which was not studied before so do not show CS-0098 and instead show CS-001 Computer Programming

similarly:

MT-002 has the prerequisite class MT-001 which was studied but was failed. So, do not show MT-002 but MT-001.

Here is the query I used to generate the result but produces wrong result:

SELECT 
    cl.class_id,cr.course_id,cr.course_code,cr.course_name,cr.course_credit 
FROM class cl 
LEFT JOIN course cr 
    ON cl.class_course=cr.course_id 
LEFT JOIN course_offer co 
    ON co.course_code=cr.course_code 
LEFT JOIN class_student cs 
    ON cl.class_id=cs.class_id 
    AND cs.student_id='S-14-1' 
    AND cl.class_status=1 
    AND co.co_semester<=2 
WHERE 
    cs.class_id IS NULL

Here is the query output:

class_id    course_id   course_code course_name           course_credit
   3           1          MT-001    Calculust 1                 3
   4           2          MT-002    Calculus 2                  3
   5           6          CS-098    Advance Programming         4

Here, Calculus I and Calculus II Both are displayed which should not be the case for a student in a semester, as a student can study only one class at a time.

this is what i did for joining three tables, you might get some idea from this code.

$this->db->select(array('u.name', 'u.companyname','i.phone','i.address','u.email','i.state','i.city','i.pincode','i.area','i.description','i.image', 'c.name AS categoryname'));
    $this->db->from('tbl_user as u');
    $this->db->join('tbl_userinfo as i', 'i.user_id = i.user_id');
    $this->db->join ('tbl_category as c', 'c.category_id = i.service_category');
    $this->db->group_by(array('u.user_id')); 
    $this->db->where(array('u.group_id' => 16));        
    $query = $this->db->get ();
    return $query->result();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM