每个月计算数据

时间:2016-12-13 06:38:13

标签: php mysql html5 codeigniter

嗨,大家好我已经在其他问题上研究了这个,但我没有得到答案,所以决定在这里问。希望你能帮助我们。 这是问题/问题:

我有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
?>

下面有&#39;我的数据库的结构:

Table 1: tblCourse
Fields: ID, Course_name, Category, Training_days

Table 2: tblData
Fields: ID, Trainees_name, Course_taken, Date_start, Date_end

谢谢你们希望你能帮助我!

对不起我的新手代码,我刚刚开始使用php开发..

编辑:伙计们我可以在一个查询中查询吗?如是?你能告诉我怎么样吗?谢谢!

2 个答案:

答案 0 :(得分:0)

Course_taken视为表tblCourse的外键。

select tblCourse.Course_name,
       Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,(Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov+Dec) as `Total`    
from (
    select 
    tblCourse.Course_name+

        sum(CASE WHEN month(tblData.Date_start)=1 or month(tblData.Date_end)=1  THEN 1 else 0 end) as `Jan`,
        sum(CASE WHEN month(tblData.Date_start)=2 or month(tblData.Date_end)=2  THEN 1 else 0 end) as `Feb`,
        sum(CASE WHEN month(tblData.Date_start)=3 or month(tblData.Date_end)=3  THEN 1 else 0 end) as `Mar`,
        sum(CASE WHEN month(tblData.Date_start)=4 or month(tblData.Date_end)=4  THEN 1 else 0 end) as `Apr`,
        sum(CASE WHEN month(tblData.Date_start)=5 or month(tblData.Date_end)=5  THEN 1 else 0 end) as `May`,
        sum(CASE WHEN month(tblData.Date_start)=6 or month(tblData.Date_end)=6  THEN 1 else 0 end) as `Jun`,

        sum(CASE WHEN month(tblData.Date_start)=7 or month(tblData.Date_end)=7  THEN 1 else 0 end) as `Jul`,
        sum(CASE WHEN month(tblData.Date_start)=8 or month(tblData.Date_end)=8  THEN 1 else 0 end) as `Aug`,
        sum(CASE WHEN month(tblData.Date_start)=9 or month(tblData.Date_end)=9  THEN 1 else 0 end) as `Sep`,
        sum(CASE WHEN month(tblData.Date_start)=10 or month(tblData.Date_end)=10 THEN 1 else 0 end) as `Oct`,
        sum(CASE WHEN month(tblData.Date_start)=11 or month(tblData.Date_end)=11 THEN 1 else 0 end) as `Nov`,
        sum(CASE WHEN month(tblData.Date_start)=12 or month(tblData.Date_end)=12 THEN 1 else 0 end) as `Dec`

    from tblCourse
    left join tblData on tblCourse.id=tblData.Course_taken
    group by tblCourse.id
) data

注意:确保Date_startDate_end应该是同一年。

答案 1 :(得分:-1)

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class ValidatorDemo {

    public static void main(String[] args) {

      System.out.println("student.xml validates against studentRule.xsd? "+validateXMLSchema("studentRule.xsd", "student.xml"));

      }

    public static boolean validateXMLSchema(String xsdPath, String xmlPath){

        try {
            SchemaFactory factory = 
                    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File(xsdPath));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(xmlPath)));
        } catch (IOException | SAXException e) {
            System.out.println("Exception: "+e.getMessage());
            return false;
        }
        return true;
    }
}