简体   繁体   English

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

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

I have two tables that store data for students - the students table and student_session table我有两个存储学生数据的表——students 表和student_session

students table structure学生表结构

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 table structure 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;

Now, I've been able to get the total number of students in a class using this query现在,我已经能够使用此查询获得 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');
    }

However, there are some students who have been disabled for non-payment of school fees or those that have left the school permanently.但是,也有部分学生因不交学费而致残或永久离校。 The problem is since these students were only disabled and not deleted, the query still counts them to the active students.问题是由于这些学生只是被禁用而不是删除,查询仍然将他们计入活跃学生。

Now, I want to exclude the disabled students from being counted.现在,我想将残疾学生排除在统计之外。

Note - in the students table structure, the 'is_active' row stores data for if a student is active or disabled.注意- 在学生表结构中, “is_active”行存储学生是活跃还是禁用的数据。 'yes' means a student is active, 'no' means a student has been disabled. “是”表示学生处于活动状态,“否”表示学生已被禁用。

How do I go about this?我该如何 go 关于这个?

You should change the is_active field to boolean.您应该将is_active字段更改为 boolean。

Join the student table with student_session.student_id = students.student_id , and filter by the is_active field in student_session使用student_session.student_id = students.student_id加入学生表,并通过 student_session 中的is_active字段进行过滤

Try this for active students in table students为表学生中的活跃学生试试这个

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
}

Try this for active students in table student_session为表 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');
}

Or a little more succinctly, you can just use the mysql COUNT() function with AS :或者更简洁一点,您可以将 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.

相关问题 如何根据学生的总分获得学生的位置 - How can I get students position based on their total score laravel 9 如何获取未登录用户的总访问次数 - How to get the total number of visits by the people who are not logged in laravel 9 如何使用 eloquent 根据 laravel 中的 Class 级别计算学生表中的男性、女性和总学生? - How do I count male, female and total students from Student table based on Class level in laravel using eloquent? 我想列出class中没有注册的同学 - I want to list the students who are not registered in the class 如何在for循环中获取给定月份所有天的记录总数 - How do i get total number of records for all days of a given month inside a for loop 如何使用php从此JSON数组中获取注释总数? - How do i get the total number of comments from this JSON array using php? 如何获得使用Facebook API和PHP的朋友总数? - How do I get the total number of friends using the Facebook API and PHP? 在 PHP 中,如何获得两个时间戳之间准确(而非近似)的周数、月数和年数? - In PHP, how do I get the accurate (not approximate) total individual number of weeks, months and years between two timestamps? 如何获取下拉菜单中所选人员的详细信息? - How do i get the details of person who is selected in drop down? 如何获得喜欢帖子的用户列表? - how do I get the list of users who liked post?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM