在php中加入mysql语句中的4个表

时间:2016-02-12 17:29:02

标签: php mysql

我不知道如何解释它但我想要的是我想在我的主题表中显示教师姓名和课程标题以及主题的日期和时间,首先我可以用其讲师显示主题名称和课程名称,但当我显示它的日期和时间。它不适合我想要的输出..

这是我的示例代码:

//subjectClass.php
public function subjects(){
            global $db;

    $sql = "
    SELECT s.*
         , i.first_name
         , i.mid_name
         , i.last_name
         , c.course_title
         , d.sub_day
         , d.start_time
         , d.end_time 
      FROM subjects_tbl s
      LEFT 
      JOIN instructors_tbl i
        ON i.instructor_id = s.instructor_id 
      LEFT 
      JOIN courses_tbl c
        ON c.course_id = s.course_id
      LEFT 
      JOIN subject_day_tbl d
        ON d.subject_id = s.subject_id;
      ";

            $query = $db->query($sql);
            if($query->num_rows > 0){
                while($row = $query->fetch_assoc()){
                    $list[] = $row;
                }
            }else{
                $list = NULL;
            }

            return $list;
        }
//subjects.php
//include 'subjectsClass.php';
<table class="table table-condensed table-bordered">
        <thead>
          <tr>
            <th>Subject Code</th>
            <th>Subject Title</th>
            <th>Unit</th>
            <th>Section</th>         
            <th>Course</th>
            <th>Instructor</th>
            <th>Day/Time</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <?php
            $subjectsClass = new subjectsClass;
            $subjects = $subjectsClass->subjects();

            foreach ($subjects as $key => $value) {

          ?>
          <tr>
            <td><?php echo $value['subject_code']; ?></td>
            <td><?php echo $value['subject_title']; ?></td>
            <td><?php echo $value['unit']; ?></td>
            <td><?php echo $value['section']; ?></td>
            <td><?php echo $value['course_title']; ?></td>
            <td><?php echo $value['first_name'] . " " . $value['mid_name'] . ". " . $value['last_name'] ; ?></td>
            <td>
              <?php echo $value['sub_day'] . " [" . $value['start_time'] . " - " . $value['end_time'] . "]<br />"; ?>
            </td>
            <td><a href="#">Edit</a> | <a href="#">Delete</a></td>
          </tr>
          <?php
              }

          ?>
        </tbody>

这是输出:

Subject Code Subject Title                  Unit Section Course Instructor              Day/Time
ITE 131      Security Issues and Principles    3 IT-R    BSIT   Darwin Paradela. Galudo Monday    [07:30:00 - 09:00:00]
ITE 131      Security Issues and Principles    3 IT-R    BSIT   Darwin Paradela. Galudo Wednesday [08:30:00 - 10:00:00]
ITE 050      Database Management System 2      3 IT-R    BSIT   Ronnie Pitt. Cambangay  Tuesday   [07:00:00 - 08:30:00]
ITE 050      Database Management System 2      3 IT-R    BSIT   Ronnie Pitt. Cambangay  Thursday  [07:00:00 - 08:30:00]

我想要的输出就是这个:

+--------------+--------------------------------+------+---------+--------+-------------------------+---------------------------------+
| Subject Code | Subject Title                  | Unit | Section | Course | Instructor              | Day/Time                        |
+--------------+--------------------------------+------+---------+--------+-------------------------+---------------------------------+
| ITE 131      | Security Issues and Principles |    3 | IT-R    | BSIT   | Darwin Paradela. Galudo | Monday    [07:30:00 - 09:00:00] |
|              |                                |      |         |        |                         | Wednesday [08:30:00 - 10:00:00] |
+--------------+--------------------------------+------+---------+--------+-------------------------+---------------------------------+
| ITE 050      | Database Management System 2   |    3 | IT-R    | BSIT   | Ronnie Pitt. Cambangay  | Tuesday   [07:00:00 - 08:30:00] |
|              |                                |      |         |        |                         | Thursday  [07:00:00 - 08:30:00] |
+--------------+--------------------------------+------+---------+--------+-------------------------+---------------------------------+

我的桌子: subjects_tbl enter image description here courses_tbl enter image description here instructors_tbl enter image description here subject_day_tbl enter image description here

1 个答案:

答案 0 :(得分:1)

I hope I can help you.

Your issue seems to be the left join of subject_day_tbl. It is a one-to-many relationship (ie, there can be many records in subject_day_tbl for each record in subjects_tbl), and when you left join a one-to-many you'll get a duplicate of the "one" side for each row in the "many" side. The only way that you could do this in a single query is by using a subquery or group statement to concatenate the rows in the database engine... but that is really bad because you're mixing display with your data model.

While it is true in general that for performance one should avoid issuing too many queries to the database, premature optimization is the root of all evil. First, try to have clean, understandable code, then look for bottlenecks if you're having issues.

In this case, a second query is certainly best. This is how I would do it:

//subjectClass.php
protected function subject_days($subject_id)
{
   // I don't know what type of object $db is it looks like ezSQL,
   // but you get the idea
   global $db;
   $sql = "SELECT sub_day, start_time, end_time
           FROM subject_day_tbl
           WHERE subject_id = %s";
   $query = $db->query($db->prepare($sql, $subject_id));
   return ($query->num_rows > 0) ? $stmt->fetch_assoc() : array();
}

public function subjects()
{
    global $db;
    $sql = "SELECT s.*
              , i.first_name
              , i.mid_name
              , i.last_name
              , c.course_title
              , d.sub_day
              , d.start_time
              , d.end_time 
            FROM subjects_tbl s
            LEFT JOIN instructors_tbl i
                   ON i.instructor_id = s.instructor_id 
            LEFT JOIN courses_tbl c
                   ON c.course_id = s.course_id
            ";
    $list = array();
    $query = $db->query($sql);
    if($query->num_rows > 0){
        while($row = $query->fetch_assoc()){
            $row['course_days'] = $this->subject_days($row['subject_id']);
            $list[] = $row;
        }
    }
    return empty($list) ? NULL : $list;
}

// subjects.php
$subjectsClass = new subjectsClass;
$subjects = $subjectsClass->subjects();
foreach ($subjects as $key => $value) {
    ?><tr>
        <td><?php echo $value['subject_code']; ?></td>
        <td><?php echo $value['subject_title']; ?></td>
        <td><?php echo $value['unit']; ?></td>
        <td><?php echo $value['section']; ?></td>
        <td><?php echo $value['course_title']; ?></td>
        <td><?php echo $value['first_name'] . " "
                     . $value['mid_name'] . ". "
                     . $value['last_name'] ; ?></td>
        <td><?php foreach($value['course_days'] as $day) {
            echo $value['sub_day'] . " [" . $value['start_time'] . " - " . $value['end_time'] . "]<br />";
        }?></td>
        <td><a href="#">Edit</a> | <a href="#">Delete</a></td>
    </tr><?php
}

Also, not to be a pedant, but you should really be escaping your output before echoing it, eg with htmlspecialchars.