我如何获得仅活跃的学生总数
How do I get the total number of students who are only active
我有两个 table 存储学生数据 - students table 和 student_sessiontable
学生table结构
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 结构
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');
}
但是,也有一些学生因不交学费而致残或永久离开学校。
问题是因为这些学生只是被禁用而没有被删除,查询仍然将他们计入活跃学生。
现在,我想把残疾学生排除在外。
注意 - 在学生table结构中,'is_active' 行存储学生是活动还是禁用的数据。 'yes'表示一个学生处于活动状态,'no'表示一个学生已被禁用。
我该怎么做?
您应该将 is_active 字段更改为布尔值。
加入学生 table 与 student_session.student_id = students.student_id,并按 is_active 筛选student_session
中的字段
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
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()
函数与 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;
}
我有两个 table 存储学生数据 - students table 和 student_sessiontable
学生table结构
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 结构
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');
}
但是,也有一些学生因不交学费而致残或永久离开学校。 问题是因为这些学生只是被禁用而没有被删除,查询仍然将他们计入活跃学生。
现在,我想把残疾学生排除在外。
注意 - 在学生table结构中,'is_active' 行存储学生是活动还是禁用的数据。 'yes'表示一个学生处于活动状态,'no'表示一个学生已被禁用。
我该怎么做?
您应该将 is_active 字段更改为布尔值。
加入学生 table 与 student_session.student_id = students.student_id,并按 is_active 筛选student_session
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
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()
函数与 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;
}