2017-01-07 2 views
1

Привет, это запрос, чтобы получить количество разных данных из разных таблиц.Как построить вложенный запрос в 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 с помощью класса CodeIgniter строителя, так что может кто-то наставит меня пожалуйста ..

Цель работы одного запроса, чтобы минимизировать попадание базы данных.

Заранее спасибо .. !!!

ответ

2

Вы можете использовать: 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 ... 

и использовать для других.

Смежные вопросы