简体   繁体   中英

MySQL CodeIgniter Select or not from second table based on column of first Select

I have 3 tables the students which has students.id, students.name

A table called students_classes which has students_classes.students_id and students_classes.classes_id and is for linking the students with their classes.

And a table named class, which has class.id, class.title, class.begin , class.end and class.type. class.type can be A or B.

A is nothing special, but B means that the class has divisions, and that the time in the table actually shows when the first division starts and when the last ends.

That is wrong though because the students are split into divisions for each class and I need the time from a single division and not all divisions.

I thought about creating another table named divisions with divison.id, division.begin, division.end and division.class_id as foreign of class.id.

Now my problem is, how do I create a select or a condition that checks the class and IF class.type is B, goes to divisions and takes the time from there instead of from class.begin and class.end?

My select function before the implementation of the "divisions" part:

function select_classes($student_id)
{
    $this->db->select('class.title as t, class.type as tp,
    class.begin as b, class.end as e');
    $this->db->from('class');
    $this->db->join('students_classes', 'class.id = students_classes.class_id');
    $this->db->join('students', 'students.id = students_classes.students_id');
    $this->db->where('students.id', $student_id);
    $query = $this->db->get();
    if ($query->num_rows() >= 1) {
        return $query->result();
    }
}

What this does is take a student's id and returns what classes he has and at what time. I now want it to also take into account if the class.type is B and if it is, it should join the divisions table and take times from it instead of the class table.

Also is it possible to make it return the changed begin/end times (if it uses the division table) as b and e like the starting statment? (because I call them as class->b, class->e in the view)

PS: Im also assuming that there will be a new students_classes.division_id column in the students_classes in order to link the students with their respective divisions as the students_classes is used for linking the stuends with their classes...

1) Keeping times in 2 different tables will not be a good option.
2) Assuming the type A's to be in some particular division (same for all A's).
3) Now collect all the classes and their divisions and all of their timings (basically all possible combination ) in one table and link your students_classes table to it.

Reframed your schema as (SQL Fiddle) ,

1) students

| id |      name |
|----|-----------|
|  1 |  Student1 |
|  2 |  Student2 |
|  3 |  Student3 |
|  4 |  Student4 |
|  5 |  Student5 |
|  6 |  Student6 |
|  7 |  Student7 |
|  8 |  Student8 |
|  9 |  Student9 |
| 10 | Student10 |

2) classes

| id |  title | type |
|----|--------|------|
|  1 | class1 |    A |
|  2 | class2 |    B |
|  3 | class3 |    B |
|  4 | class4 |    A |
|  5 | class5 |    A |
|  6 | class6 |    A |
|  7 | class7 |    B |

3) divisions

| id | title |
|----|-------|
|  1 |  div1 |
|  2 |  div2 |
|  3 |  div3 |

4) classes_divisions

| id | class_id | division_id | begin |   end |
|----|----------|-------------|-------|-------|
|  1 |        1 |           1 | 07:30 | 13:30 |
|  2 |        2 |           1 | 08:30 | 14:30 |
|  3 |        2 |           2 | 08:00 | 14:00 |
|  4 |        3 |           2 | 07:00 | 12:00 |
|  5 |        3 |           3 | 06:30 | 11:30 |
|  6 |        4 |           1 | 07:15 | 12:15 |
|  7 |        5 |           1 | 06:20 | 12:20 |
|  8 |        6 |           1 | 06:50 | 12:50 |
|  9 |        6 |           1 | 07:50 | 13:50 |
| 10 |        6 |           2 | 08:50 | 14:50 |
| 11 |        6 |           3 | 09:50 | 15:50 |

5) students_classes

| id | student_id | class_division_id |
|----|------------|-------------------|
|  1 |          1 |                 1 |
|  2 |          2 |                 2 |
|  3 |          3 |                 2 |
|  4 |          4 |                 3 |
|  5 |          5 |                 4 |
|  6 |          6 |                 5 |
|  7 |          7 |                 6 |
|  8 |          8 |                 6 |
|  9 |          9 |                 6 |
| 10 |         10 |                 7 |

Now a simple JOIN with knowing only the student_id ,

SELECT classes.title           AS t, 
       classes.type            AS tp, 
       classes_divisions.begin AS b, 
       classes_divisions.end   AS e 
FROM   students_classes 
       JOIN classes_divisions 
         ON students_classes.class_division_id = classes_divisions.id 
       JOIN classes 
         ON classes_divisions.class_id = classes.id 
WHERE  students_classes.student_id = 2;

OUTPUT:

| t      | tp   | b     | e     |
|--------|------|-------|-------|
| class2 |    B | 08:30 | 14:30 |

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