如何计算具有特定值的数据库中的列数(查询)

时间:2019-05-15 02:39:04

标签: mysql sql codeigniter chart.js

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

这里是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();
}

但是结果是查询仍然会计算每年的学生总数,我所希望的是我可以在student表的status列中计算值1。

您的帮助对我很重要,谢谢!抱歉,如果我的问题写得不是很整洁,并且为我的英语不好而表示歉意……真是爱慕约翰·哈罗德(John Harold)。

4 个答案:

答案 0 :(得分:1)

您的问题是您正在this is the errors that my program throws me: Traceback (most recent call last): File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\managers.py", line 1651, in create_block_manager_from_blocks placement=slice(0, len(axes[0])))] File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\blocks.py", line 3095, in make_block return klass(values, ndim=ndim, placement=placement) File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\blocks.py", line 87, in __init__ '{mgr}'.format(val=len(self.values), mgr=len(self.mgr_locs))) ValueError: Wrong number of items passed 6, placement implies 11 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "Regresion.py", line 120, in <module> X = pd.DataFrame(data= principalComponents, columns = labels) File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 424, in __init__me.py", line 424, in __init__ copy=copy) ernals\construction.py", line 167, in init_ndarray File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\construction.py", line 167, in init_ndarray ernals\managers.py", line 1660, in create_block_manager_from_blocks return create_block_manager_from_blocks([values], [columns, index]) File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\managers.py", line 1691, in construction_errorernals\managers.py", line 1660, in create_block_manager_from_blocks construction_error(tot_items, blocks[0].shape[1:], axes, e) File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\managers.py", line 1691, in construction_error passed, implied)) ValueError: Shape of passed values is (458, 6), indices imply (458, 11) 使用布尔表达式:COUNT,它将始终计数为1(除非a.status=1为null),无论该表达式是true还是false。与其a.status不使用这些值,不如COUNT

SUM

答案 1 :(得分:0)

如果您的学生表的状态仅为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

只希望它对您有用。

答案 2 :(得分:0)

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

$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 :(得分:0)

git push