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
In pure SQL, the query you want is:
All you need to do is translate this CodeIgniter – as suggested by @Broncha, you can do: