【问题标题】:How do I get the total number of students who are only active如何获得仅活跃的学生总数
【发布时间】:2021-06-10 16:32:54
【问题描述】:

我有两个存储学生数据的表 - 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;

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

 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' 行存储学生是活跃还是禁用的数据。 “是”表示学生处于活动状态,“否”表示学生已被禁用。

我该怎么做?

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    或者更简洁一点,你可以只使用mysql的COUNT()函数和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;
        }
    

    【讨论】:

    • 正在从 student_session 中计算学生,但正在从学生表中禁用学生。怎么join两张表,得到is_active记录?
    • 我更新了我的回复。这应该可以工作,虽然我不能在这里测试。
    【解决方案2】:

    您应该将 is_active 字段更改为布尔值。

    使用 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');
    }
    

    【讨论】:

    • 正在从 student_session 中计算学生,但正在从学生表中禁用学生。怎么join两张表,得到is_active记录?
    • student_session 应该是会话中的活跃用户,studets 应该是如果帐户被激活或禁用,你想知道什么?会话中的活动帐户或活动用户? @Mary4bills
    • 好的!我想要学生表中的活动帐户
    • 查询students表,如果你想在student表中有活动账户,为什么要使用session_students表? @Mary4bills
    • 我收到此错误消息:调用 bool 上的成员函数 num_rows()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 2023-04-08
    • 2022-08-16
    相关资源
    最近更新 更多