【问题标题】:how to calculate the number of columns in a database with specific values (Query)如何计算具有特定值的数据库中的列数(查询)
【发布时间】:2019-05-15 02:39:04
【问题描述】:

我有一个表 tbl_student 和 table year,学生从 year 表中获取学年。

这里是 tbl_student:

|id_st   | student_name  |  year  | status |
--------------------------------------------
| 501    | John Carlton  |   1    |    1   |
--------------------------------------------
| 502    | Harold Louis  |   2    |    1   |
--------------------------------------------
| 503    | Jackson F     |   2    |    0   |
--------------------------------------------

这里是 tbl_year :

|id_year | year_name | 
----------------------
| 1      | 2001      |
----------------------   
| 2      | 2002      |
----------------------  
| 3      | 2003      |
----------------------  

我想要的是我可以计算每年活跃学生的数量(列状态值 = 1),我试图用我的查询来计算它,但我得到的是计算每年的学生数量,这里是我当前的查询:

公共函数 getStudentActive(){

    $sql = "SELECT b.year_name as year, count(a.status=1) as total from tbl_student as a left join tbl_year as b on b.id_year=a.year GROUP BY a.tbl_year ORDER BY year ASC" ;
    return $this->db->query($sql)->result();
}

但是结果是查询还是统计了每年的学生总数,我想要的只是可以在学生表的status列中计算出值1。

您的帮助对我来说意义重大,谢谢!对不起,如果我的问题写得不整洁,对不起我的英语不好......爱〜约翰哈罗德。

【问题讨论】:

    标签: mysql sql codeigniter chart.js


    【解决方案1】:

    你的问题是你COUNT是一个布尔表达式:a.status=1,它总是计数为1(除非a.status为空),不管表达式是真还是假。而不是COUNTing 这些值,而是SUM 它们:

    $sql = "SELECT b.year_name as year, SUM(a.status=1) as total 
            from tbl_student as a 
            left join tbl_year as b on b.id_year=a.year
            GROUP BY a.tbl_year 
            ORDER BY year ASC" ;
    

    【讨论】:

      【解决方案2】:

      在控制器中添加以下代码:

      $activeStudents = $this->student_model->count_student_active(array('status'=>1);
      

      在 student_model 中添加此代码:

      public function count_student_active($where)
         {
      
             $this->db->select();
      
             $this->db->from('tbl_student');
      
             $query = $this->db->get_where('', $where);
      
             $result = $query->num_rows();
      
             return $result;
      
         }
      

      【讨论】:

        【解决方案3】:

        如果您的学生表只有 1&0 状态,则状态列的总和值就是您想要的结果,即每年活跃学生的数量。如下所示:

        select
            a.year_name,
            sum(b.status) as active_student_count
        from
            tbl_year a
        left join
            tbl_student b on a.id_year = b.year
        group by
            a.year_name
        

        如果你的学生状态不只是两个值,还有其他状态,比如 2,3,4...并且只有 1 代表活动状态,你可以在你的 SQL 中使用“case when”表达式,如下所示:

        select
            a.year_name,
            sum(case when b.status=1 then 1 else 0 end) as active_student_count
        from
            tbl_year a
        left join
            tbl_student b on a.id_year = b.year
        group by
            a.year_name
        

        希望它对你有用。

        【讨论】:

          【解决方案4】:
          $sql = "SELECT count(*),year.year_name FROM `student` INNER JOIN year on student.year = year.id where student.status = '1' GROUP BY student.year" ;
          return $this->db->query($sql)->result();
          

          【讨论】:

          • 不要在 StackOverflow 上仅发布代码答案。包括解释和代码。这将对未来的访问者有所帮助。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-13
          • 2021-03-15
          • 2015-10-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多