如何在 codeigniter 中构建嵌套查询?
How to build nested query in codeigniter?
您好,下面是从不同表中获取不同数据计数的查询。
select(
select count(*)
from teachers
where teacher_status = 1
)as teacher_count,
(
select count(*)
from students
where student_status = 1
)as students_count,
(
select count(*)
from housekeepers
where housekeeper_status = 1
)as housekeeping_count,
(
select count(*)
from students
where student_status = 1 and
gender = "Male"
) as total_male_student_count,
(
select count(*)
from students
where student_status = 1 and
gender = "Female"
) as total_female_student_count
现在我想在 codeigniter builder class 的帮助下在 codeigniter 中构建这个单一查询,所以请有人指导我..
运行 单个查询的目的是尽量减少数据库命中。
提前致谢..!!!
您可以使用:get_compiled_select
像这样
$this->db->select('count(*) as count');
$this->db->from('teacher_status');
$teacher_status = $this->db->get_compiled_select();
$this->db->select("select($teacher_status)as teacher_count, ... ");
this->db ...
并用于其他人。
您好,下面是从不同表中获取不同数据计数的查询。
select(
select count(*)
from teachers
where teacher_status = 1
)as teacher_count,
(
select count(*)
from students
where student_status = 1
)as students_count,
(
select count(*)
from housekeepers
where housekeeper_status = 1
)as housekeeping_count,
(
select count(*)
from students
where student_status = 1 and
gender = "Male"
) as total_male_student_count,
(
select count(*)
from students
where student_status = 1 and
gender = "Female"
) as total_female_student_count
现在我想在 codeigniter builder class 的帮助下在 codeigniter 中构建这个单一查询,所以请有人指导我..
运行 单个查询的目的是尽量减少数据库命中。
提前致谢..!!!
您可以使用:get_compiled_select
像这样
$this->db->select('count(*) as count');
$this->db->from('teacher_status');
$teacher_status = $this->db->get_compiled_select();
$this->db->select("select($teacher_status)as teacher_count, ... ");
this->db ...
并用于其他人。