如何建立笨嵌套查询?

问题描述 投票:1回答:1

嗨下面是查询,以从不同的表,在不同的数据计数。

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

现在我想建立与笨生成器类的帮助下笨这种单一的查询,这样可以有人指导我请..

运行单个查询的目的是尽量减少数据库命中。

提前致谢..!!!

php mysql codeigniter database-performance
1个回答
4
投票

你可以使用: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 ...

和他人使用。

© www.soinside.com 2019 - 2024. All rights reserved.