Php 我如何获得只活跃的学生总数
我有两个为学生存储数据的表-学生表和学生会议表 学生表结构Php 我如何获得只活跃的学生总数,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有两个为学生存储数据的表-学生表和学生会议表 学生表结构 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 D
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;
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;
学生会话表结构
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;
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');
}
然而,也有一些学生因未缴纳学费而残疾,或者永久离开学校。
问题是,由于这些学生仅被禁用而未被删除,因此查询仍将其计入活动学生
现在,我想把残疾学生排除在外
注意在students表结构中,'is_active'行存储学生处于活动或禁用状态的数据“是”表示学生处于活动状态,“否”表示学生已被禁用
如何执行此操作?您应该将处于活动状态的字段更改为布尔值。
使用学生会话加入学生表。学生id=students.student\u id,并通过学生会话中的is\u active字段进行筛选 在表中为活跃的学生尝试此选项 在table student_课程中为活跃的学生尝试此方法
或者更简洁一点,您可以将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;
}
学生将从学生会议中计数,但学生将从学生表中禁用。如何联接这两个表并获取is_活动记录?学生_会话应该是会话中的活动用户,学生组应该是会话中的活动用户。如果帐户被激活或禁用,您想知道什么?会话中的活动帐户或活动用户@玛丽·比尔索凯!我想要学生表中的活动帐户查询学生表,如果您想要学生表中的活动帐户,为什么要使用session_students表@Mary4billsI获取此错误消息:对boolstudents上的成员函数num_rows()的调用正在从student_会话中计数,但正在从students表中禁用students。如何连接这两个表并获取is_活动记录?我更新了我的响应。这应该行得通,不过我不能在这里测试。yass!!成功了。非常感谢much@Strawberry-你说得对,更新了