【发布时间】:2017-04-28 03:41:38
【问题描述】:
我已经在其他问题中对此进行了研究,但没有得到答案,所以决定在这里提问。
我有 2 个表,即 tblCourse 和 tblData,我一直将我的所有课程列表存储在 tblCourse 中,而在 tblData 中我存储人名和他们所学的课程。现在,我想做的是,我想获得每个月参加特定课程的所有人的总数。
例如:
----------------------------------------------------------------------------
Course | Jan | feb | Mar | Apr | May | ......................| Dec | Total |
----------------------------------------------------------------------------
Course1 | 2 | 3 | 0 | 0 | 1 | ......................| 2 | 8 |
----------------------------------------------------------------------------
Course2 | 2 | 3 | 2 | 0 | 1 | ......................| 2 | 10 |
----------------------------------------------------------------------------
Course3 | 2 | 3 | 1 | 0 | 1 | ......................| 2 | 9 |
----------------------------------------------------------------------------
这是我的代码:
<?php
$query = $this->db->query("SELECT * FROM tblcourse where category = 'Soft' and inex = 'inhouse' ORDER by course_name ASC");
foreach ($query->result() as $row){
?>
<tr>
<td>Id</td>
<td><?php echo $row->course_name; ?></td> // Output the Course Name
<td class="center"><?php echo $row->days; ?></td> //Output the Training days
<?php
$course = $row->course_name;
$query_jan = $this->db->query("SELECT * FROM tbldata where course like '%$course%' and course_end < '2016-11-30' and course_end >= '2016-11-01'");
?>
<td><?php echo $query_jan->num_rows(); ?></td> // Output the corresponding number of course taken by the person for the month of january
<? php
} // end of foreach
?>
这是我的数据库的结构:
Table 1: tblCourse
Fields: ID, Course_name, Category, Training_days
Table 2: tblData
Fields: ID, Trainees_name, Course_taken, Date_start, Date_end
我刚刚开始使用 php 进行开发。
我可以在单个查询中查询吗?
【问题讨论】:
-
tblCourse和tblData是什么关系?如果您有关系,那么您可以执行 SQL-JOIN 并获得您想要的结果... -
你能告诉我怎么做吗?让我们假设 course_taken 是来自 tbldata 的外键
标签: php html mysql codeigniter