如何在codeigniter中构建嵌套查询?

Man*_*dan 2 php mysql codeigniter database-performance

嗨,下面是从不同表中获取不同数据计数的查询。

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
Run Code Online (Sandbox Code Playgroud)

现在我想在 codeigniter 构建器类的帮助下在 codeigniter 中构建这个单个查询,所以有人可以指导我吗..

运行单个查询的目的是最小化数据库命中。

提前致谢..!!!

aya*_*aya 6

您可以使用: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 ...
Run Code Online (Sandbox Code Playgroud)

并为他人使用。