2014-05-22 92 views
0

我想將此子查詢更改爲CodeIgniter。如何使用規則CodeIgniter進行此查詢。CodeIgniter - 更改子查詢

select *, 
      mahasiswa.nim AS id, (select count(id_absen) 
      from absen ABS 
      where ABS.nim = mahasiswa.nim 
      and tanggal >= DATE_SUB('$tgl_tutup_ujian', INTERVAL 3 MONTH) and tanggal <= '$tgl_tutup_ujian') AS jml_absen, 
      (select count(id_bayar) from pembayaran_iuran where ((bulan=MONTH(DATE_SUB('$tgl_tutup_ujian', INTERVAL 3 MONTH)) 
      and tahun=YEAR(DATE_SUB('$tgl_tutup_ujian', INTERVAL 3 MONTH))) 
      or (bulan=MONTH(DATE_SUB('$tgl_tutup_ujian', INTERVAL 2 MONTH)) 
      and tahun=YEAR(DATE_SUB('$tgl_tutup_ujian', INTERVAL 2 MONTH))) 
      or (bulan=MONTH(DATE_SUB('$tgl_tutup_ujian', INTERVAL 1 MONTH)) 
      and tahun=YEAR(DATE_SUB('$tgl_tutup_ujian', INTERVAL 1 MONTH)))) 
      AND pembayaran_iuran.nim = mahasiswa.nim) 
      AS jml_bayar from mahasiswa 
      left join pendaftaran_ukt on pendaftaran_ukt.nim = mahasiswa.nim 
      where nama like '$nama' 

我嘗試這樣,但不工作。

$this->db->select('select *,mahasiswa.nim AS id, (select count(id_absen)'); 
     $this->db->from('absen ABS'); 
     $this->db->where('ABS.nim = mahasiswa.nim 
      and tanggal >= DATE_SUB('$tgl_tutup_ujian', INTERVAL 3 MONTH) and tanggal <= '$tgl_tutup_ujian') AS jml_absen'); 
     $this->db->select(' (select count(id_bayar)'); 
     $this->db->from('pembayaran_iuran'); 
     $this->db->where('((bulan=MONTH(DATE_SUB('$tgl_tutup_ujian', INTERVAL 3 MONTH)) 
      and tahun=YEAR(DATE_SUB('$tgl_tutup_ujian', INTERVAL 3 MONTH))) 
      or (bulan=MONTH(DATE_SUB('$tgl_tutup_ujian', INTERVAL 2 MONTH)) 
      and tahun=YEAR(DATE_SUB('$tgl_tutup_ujian', INTERVAL 2 MONTH))) 
      or (bulan=MONTH(DATE_SUB('$tgl_tutup_ujian', INTERVAL 1 MONTH)) 
      and tahun=YEAR(DATE_SUB('$tgl_tutup_ujian', INTERVAL 1 MONTH)))) 
      AND pembayaran_iuran.nim = mahasiswa.nim) 
      AS jml_bayar from mahasiswa '); 
     $this->db->join('pendaftaran_ukt','pendaftaran_ukt.nim = mahasiswa.nim','left'); 
     $this->db->order_by('id_ukt','asc'); 
     $this->db->limit($limit, $offset); 

我在做什麼錯?

回答

0

CodeIgniter db庫不支持子查詢操作,您可以通過$this->db->query()來管理查詢操作。

PS:您還可以安裝子查詢操作庫, Sub query library

看到更多的細節Click here