繁体   English   中英

如何获得仅活跃的学生总数

[英]How do I get the total number of students who are only active

我有两个存储学生数据的表——students 表和student_session

学生表结构

CREATE TABLE IF NOT EXISTS `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) NOT NULL,
  `admission_no` varchar(100) DEFAULT NULL,
  `roll_no` varchar(100) DEFAULT NULL,
  `admission_date` date DEFAULT NULL,
  `firstname` varchar(100) DEFAULT NULL,
  `lastname` varchar(100) DEFAULT NULL,
  `rte` varchar(20) DEFAULT NULL,
  `image` varchar(100) DEFAULT NULL,
  `mobileno` varchar(100) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  `is_active` varchar(255) DEFAULT 'yes',
  `disable_at` date NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

student_session 表结构

CREATE TABLE IF NOT EXISTS `student_session` (
  `id` int(11) NOT NULL,
  `session_id` int(11) DEFAULT NULL,
  `student_id` int(11) DEFAULT NULL,
  `class_id` int(11) DEFAULT NULL,
  `section_id` int(11) DEFAULT NULL,
  `route_id` int(11) NOT NULL,
  `hostel_room_id` int(11) NOT NULL,
  `vehroute_id` int(10) DEFAULT NULL,
  `transport_fees` float(10,2) NOT NULL DEFAULT 0.00,
  `fees_discount` float(10,2) NOT NULL DEFAULT 0.00,
  `is_active` varchar(255) DEFAULT 'no',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

现在,我已经能够使用此查询获得 class 中的学生总数

 public function Gettotalstudents($session_id, $section_id, $class_id) 
    {
        $this->db->select('student_id');
        $this->db->where('session_id', $session_id);
        $this->db->where('section_id', $section_id);
        $this->db->where('class_id', $class_id);
        return $this->db->count_all_results('student_session');
    }

但是,也有部分学生因不交学费而致残或永久离校。 问题是由于这些学生只是被禁用而不是删除,查询仍然将他们计入活跃学生。

现在,我想将残疾学生排除在统计之外。

注意- 在学生表结构中, “is_active”行存储学生是活跃还是禁用的数据。 “是”表示学生处于活动状态,“否”表示学生已被禁用。

我该如何 go 关于这个?

您应该将is_active字段更改为 boolean。

使用student_session.student_id = students.student_id加入学生表,并通过 student_session 中的is_active字段进行过滤

为表学生中的活跃学生试试这个

public function Gettotalstudents($session_id, $section_id, $class_id) 
{
    $this->db->select('student_session.student_id');
    $this->db->from("student_session,students"); //session table and student table
    $this->db->where('student_session.student_id', 'student_id.student_id'); //join tables
    $this->db->where('student_session.session_id', $session_id);
    $this->db->where('student_session.section_id', $section_id);
    $this->db->where('student_session.class_id', $class_id);
    $this->db->where('student_id.is_active', 'yes'); //only active
    return $this->db->count_all_results(); // count active
}

为表 student_session 中的活跃学生试试这个

 public function Gettotalstudents($session_id, $section_id, $class_id) 
{
    $this->db->select('student_id');
    $this->db->where('session_id', $session_id);
    $this->db->where('section_id', $section_id);
    $this->db->where('class_id', $class_id);
    $this->db->where('is_active', 'yes');
    return $this->db->count_all_results('student_session');
}

或者更简洁一点,您可以将 mysql COUNT() function 与AS一起使用:

public function Gettotalstudents($session_id, $section_id, $class_id) 
    {
        $this->db->select('COUNT(ss.student_id) as total');
        $this->db->from('student_session ss');
        $this->db->join('students st', 'st.id = ss.student_id');
        $this->db->where(array('st.is_active'=> 'yes','ss.session_id' => $session_id, 'ss.section_id' => $section_id, 'ss.class_id' => $class_id));
        return $this->db->get()->row()->total;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM