Part of the EllisLab Network
This thread is a discussion for the wiki article: Subqueries
   
2 of 2
2
Subqueries
Posted: 27 September 2011 01:47 AM   [ Ignore ]   [ # 16 ]  
Summer Student
Total Posts:  7
Joined  07-14-2011

The new library works.. but, there’s a problem..
This is my code:

function arr_tunggakan_kontan($nis$nama_tagihan{
 
        $this
->db->select('sub1.id_jenis_tagihan, sub1.thn_ajaran, sub1.jumlah_angsur, sub1.besar');
        
$this->db->select('sub1.jumlah_angsur-IFNULL(sub2.con, 0) AS sisa_angsuran'FALSE)->select('(sub1.jumlah_angsur-IFNULL(sub2.con, 0))*sub1.besar AS tunggakan'FALSE);
        
$sub $this->subquery->start_subquery('from');
            
$sub->select('t.id_thn_ajaran, t.thn_ajaran, j.id_jenis_tagihan, j.besar, j.jumlah_angsur');
            
$sub->from('siswa AS s'FALSE);
            
$sub->join('siswa_kelas_thn_ajaran AS skt''skt.nis = s.nis');
            
$sub->join('thn_ajaran AS t''t.id_thn_ajaran = skt.id_thn_ajaran');            
            
$sub->join('kelas AS k''k.id_kelas = skt.id_kelas');
            
$sub->join('jurusan AS ju''ju.id_jurusan = k.id_jurusan');
            
$sub->join('jenis_tagihan AS j''ju.departemen = j.departemen AND j.id_thn_ajaran = skt.id_thn_ajaran AND j.jenis_kelas = k.jenis_kelas');
            
$sub->where('s.nis'$nis);
            
$sub->where('j.nama_tagihan'$nama_tagihan);
        
$this->subquery->end_subquery('sub1');   
        
$sub $this->subquery->start_subquery('join''left outer''sub1.id_jenis_tagihan = sub2.id_jenis_tagihan');
            
$sub->select(' st.id_jenis_tagihan, j.id_thn_ajaran, st.jml_angsuran')->select_sum('st.jml_angsuran''con');
            
$sub->from('siswa_tagihan AS st');
            
$sub->join('jenis_tagihan AS j''j.id_jenis_tagihan = st.id_jenis_tagihan');
            
$sub->where('st.nis'$nis)->where('j.nama_tagihan'$nama_tagihan);
            
$sub->group_by('st.id_jenis_tagihan');
        
$this->subquery->end_subquery('sub2');
        
        
$query $this->db->get()->result();
        
        return 
$query;   
    

and this is the result query:

SELECT `sub1`.`id_jenis_tagihan`, `sub1`.`thn_ajaran`, `sub1`.`jumlah_angsur`, `sub1`.`besar`, sub1.jumlah_angsur-IFNULL(sub2.con0) AS sisa_angsuran, (sub1.jumlah_angsur-IFNULL(sub2.con0))*sub1.besar AS tunggakan 
FROM 
( (
 
SELECT `t`.`id_thn_ajaran`, `t`.`thn_ajaran`, `j`.`id_jenis_tagihan`, `j`.`besar`, `j`.`jumlah_angsur
 
FROM` (`siswa` AS s) 
 JOIN 
`siswa_kelas_thn_ajaran` AS skt ON `skt`.`nis` = `s`.`nis` 
 JOIN 
`thn_ajaran` AS t ON `t`.`id_thn_ajaran` = `skt`.`id_thn_ajaran` 
 JOIN 
`kelas` AS k ON `k`.`id_kelas` = `skt`.`id_kelas` 
 JOIN 
`jurusan` AS ju ON `ju`.`id_jurusan` = `k`.`id_jurusan` 
 JOIN 
`jenis_tagihan` AS j ON `ju`.`departemen` = `j`.`departemen` AND j.id_thn_ajaran = skt.id_thn_ajaran AND j.jenis_kelas = k.jenis_kelas 
 WHERE 
`s`.`nis` = '113080130' 
 AND 
`j`.`nama_tagihan` = 'Qurban'
) AS sub1) 
LEFT OUTER JOIN (
 SELECT 
`st`.`id_jenis_tagihan``j`.`id_thn_ajaran``st`.`jml_angsuran`, SUM(`st`.`jml_angsuran`) AS `con` 
 FROM (
`siswa_tagihan` AS st) 
 JOIN 
`jenis_tagihan` AS j ON `j`.`id_jenis_tagihan` = `st`.`id_jenis_tagihan` 
 WHERE 
`st`.`nis` = '113080130' 
 AND 
`j`.`nama_tagihan` = 'Qurban' 
 GROUP BY 
`st`.`id_jenis_tagihan`
) AS sub2 ON 
`sub1`.`id_jenis_tagihan` = `sub2`.`id_jenis_tagihan` 

Strangely an ` mark showed up after FROM (at line 4). Is there something wrong with my code? Thx.. smile

Profile
 
 
Posted: 27 September 2011 08:49 AM   [ Ignore ]   [ # 17 ]  
Summer Student
Avatar
Total Posts:  24
Joined  12-26-2008
azheem - 27 September 2011 01:47 AM

Strangely an ` mark showed up after FROM (at line 4). Is there something wrong with my code? Thx.. smile

That’s a weird issue, I don’t what would be causing it.  Your code looks perfect.

I can look into it more tonight, after work.

As a thought, try removing the

FALSE); 

after

$sub->from('siswa AS s' 

I don’t think the from method has a 2nd parameter anyway, so I don’t think that does anything.

$sub->from('siswa AS s'); 

When I tested it (on CodeIgniter 2.0.2), it worked.  After work I can test it more.

Thanks for using my library, and I will get it fixed when I have time, I’m a busy man.

Profile
 
 
Posted: 01 October 2011 12:20 AM   [ Ignore ]   [ # 18 ]  
Summer Student
Total Posts:  7
Joined  07-14-2011

i’ve tried that and still doesn’t work.

Thx for ur response..

Profile
 
 
Posted: 23 November 2011 02:48 PM   [ Ignore ]   [ # 19 ]  
Summer Student
Total Posts:  3
Joined  06-24-2011

Hello,

I have a problem!
It worked on my local server, but did not work on my public web server…

My Model:

function OfertasHome($ofertas){
 $this
->db->select_sum('nu_vl_ppc');
 
$sub $this->subquery->start_subquery('from');
 
$sub->select('nu_vl_ppc'false);
 
$sub->from('tb_oferta');
 
$sub->where('nu_cidade'$ofertas['cidade']);
 
$sub->where('dt_inicio <='$ofertas['data']);
 
$sub->where('dt_fim >='$ofertas['data']);
 
$sub->where('nu_orcamento >''nu_vl_ppc');
 
$sub->where('fl_status''1');
 
$sub->order_by('nu_vl_ppc''desc');
 
$sub->limit(25);
 
$this->subquery->end_subquery('subquery'); 
 
$sql $this->db->get();
 if(
$query->num_rows() > 0){
  
return $sql->result();
 
}else{
  
return FALSE
 
}

Error on the website:

A PHP Error was encountered

Severity
Warning

Message
mysql_errno(): 29 is not a valid MySQL-Link resource

Filename
mysql/mysql_driver.php

Line Number
453
------------------------------------------------------------------------
A PHP Error was encountered

Severity
Warning

Message
mysql_error(): 29 is not a valid MySQL-Link resource

Filename
mysql/mysql_driver.php

Line Number
440
------------------------------------------------------------------------
A Database Error Occurred
Error Number
:

SELECT SUM(`nu_vl_ppc`) AS nu_vl_ppc FROM ((SELECT nu_vl_ppc FROM (tb_ofertaWHERE `nu_cidade` = '23' AND `dt_inicio` <= '2011-11-23' AND `dt_fim` >= '2011-11-23' AND `nu_orcamento` > 'nu_vl_ppc' AND `fl_status` = '1' ORDER BY nu_vl_ppc desc LIMIT 25) AS subquery)

Filename: /home/armazemdasofertas/www/admin/models/cpc_model.php

Line Number
18 

Can you help me?
Note: The query works correctly in public MySQL server.

 Signature 

CodeIgniter Website: http://www.armazemdasofertas.com.br
My blog: http://programandotododia.blogspot.com

Profile
 
 
Posted: 24 November 2011 05:49 AM   [ Ignore ]   [ # 20 ]  
Summer Student
Total Posts:  6
Joined  11-24-2011

hello, all of you ,i am new here. hope all of you have a good day

 Signature 

Aviosoft Player

Profile
 
 
Posted: 11 January 2012 09:37 AM   [ Ignore ]   [ # 21 ]  
Summer Student
Total Posts:  10
Joined  11-23-2009

NTICompass, thanks for this great library! It works like a charm and is a wonderful tool when queries start getting complex. Thanks again, man!

Profile
 
 
Posted: 01 February 2012 12:24 AM   [ Ignore ]   [ # 22 ]  
Summer Student
Total Posts:  1
Joined  02-01-2012

I’ve got a curious one using the library:
It’s escaping the offset value of the subquery.

It’s a monster of a query, here’s the relevant snippet:
ORDER BY `r1`.`date` DESC LIMIT 10, `10)` AS t)

See that the offset is getting escaped—including the closing paren?  Including that paren is totally breaking the query.

I’m simply setting it like this in my model:
$sub->limit($limit, $offset);


Any idea of what I can change to make this work properly?  I don’t really relish hacking system files, but if that’s what it takes, I will.  Any ideas would be fantastic.

Profile
 
 
Posted: 03 February 2012 05:50 AM   [ Ignore ]   [ # 23 ]  
Summer Student
Total Posts:  7
Joined  07-14-2011
tripppp - 01 February 2012 12:24 AM

I’ve got a curious one using the library:
It’s escaping the offset value of the subquery.

It’s a monster of a query, here’s the relevant snippet:
ORDER BY `r1`.`date` DESC LIMIT 10, `10)` AS t)

See that the offset is getting escaped—including the closing paren?  Including that paren is totally breaking the query.

I’m simply setting it like this in my model:
$sub->limit($limit, $offset);

Could you write your full code using the library, please? For a clearer view.. smile

Profile
 
 
Posted: 14 February 2012 11:16 PM   [ Ignore ]   [ # 24 ]  
Summer Student
Total Posts:  1
Joined  02-14-2012

  Select idddanhmuc_cap2,danhmuc_cha as cha
  From danhmuc join danhmuc_menu on danhmuc.iddanhmuc_cap2=danhmuc_menu.iddanhmuc_menu
  Where danhmuc_cha!=0 and (select vitridanhmuc_menu from danhmuc_menu where iddanhmuc_menu=cha)=‘menu’;

How to rewrite in CodeIgniter Active Record class + Subqueries class that you provide. I’ve try many case but it still doesn’t work

Thanks:-)

Profile
 
 
   
2 of 2
2