2

Hi below is the query to get count of different data from different tables.

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

Now i want to build this single query in codeigniter with the help of codeigniter builder class, so can someone guide me please..

Purpose of running single query is to minimize the database hit.

Thanks in advance..!!!

1 Answer 1

7

You can use: get_compiled_select like this

$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 ...

And use for others.

Sign up to request clarification or add additional context in comments.

1 Comment

the second select should be in a double quote not single quote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.