Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:10:58+00:00 2026-06-02T22:10:58+00:00

I am stuck with a mysql query. I have three tables: Table Name :

  • 0

I am stuck with a mysql query. I have three tables:

Table Name : Teacher

teacherid  teachername 
 1001           ABC
 1002           XYZ

Table Name : batch

batchid   batchname  batch_type     class   batchinstructor     seat
   1        Solar        1            Five     1001               40
   2        Earth        2            Six      1002               40

Table Name: student

    studentid studentname batchid
     1          Bon Jovi     1
     2          Shane Warne  2
     3          John Denver  1
     4          Steve Wuagh  2
     5          Perterson    2

What I want to achieve

 batchname class  teachername seat  students_enrolled  seat_available
   Solar   Five    ABC          40           2              38
   Earth   six     XYZ          40           3              37

I am using codeigniter and have managed to achieve all the things I have mentioned above accept for the column students_enrolled, seat_available

Here’s the code what I have tried.

      function batch_list($perPage,$uri) { 
    $this->db->select('*');
    $this->db->from('batch');
    $this->db->join('teacher', 'batch.batchinstructor = teacher.teacherid');
    $this->db->order_by('batchid','DESC');
    $getData = $this->db->get('', $perPage, $uri);
    if($getData->num_rows() > 0)
    return $getData->result_array();
        else
        return null;
    }

Would you please kindly help me with the mysql query. please note that I am using Codeigniter.

thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:10:59+00:00Added an answer on June 2, 2026 at 10:10 pm

    In pure SQL, the query you want is:

    SELECT
      b.batchname, b.class, t.teachername, b.seat,
      COUNT(s.studentid) AS students_enrolled,
      b.seat - COUNT(s.studentid) AS seat_available
    FROM batch b
    LEFT JOIN Teacher t ON t.teacherid = b.batchinstructor
    LEFT JOIN students s ON s.batchid = b.batchid
    GROUP BY s.batchid
    ORDER BY b.batchid DESC
    

    All you need to do is translate this CodeIgniter – as suggested by @Broncha, you can do:

    $result = $this->db->query($sql);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP/MySQL query that returns to an HTML table, and I'm stuck
I have a big mysql query which needs to trawl through 4 tables to
MY PLATFORM: PHP & mySQL WHAT I HAVE HERE: I have 4 tables, namely,
I have a MySQL table with id, category_id (parent id), and url. I have
I am using MySQL and I have two tables: master_table ORDERNO ITEM QTY stock_bal
I need some help with a MySQL query I'm working on. I have data
In the query I have order by name (alphabetically), stick the query in $res,
I have data split across several mysql tables to prevent data duplication but joining
I'm stuck with retrieving data from a MySQL-database in an efficient way. I have
I'm stuck on a query with a join. The client's site is running mysql4,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.