[英]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.