Part of the EllisLab Network
This thread is a discussion for the wiki article: Subqueries
   
1 of 2
1
Subqueries
Posted: 30 December 2010 04:36 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  24
Joined  12-26-2008

For any questions/comments about the subquery library, please post here.

Profile
 
 
Posted: 30 December 2010 05:20 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Avatar
Total Posts:  8
Joined  10-05-2009

Can you give an example of a join? My subquery needs to join on an id from the parent query and I can’t get it to work.

Profile
 
 
Posted: 30 December 2010 05:57 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  24
Joined  12-26-2008
dfreerksen - 30 December 2010 10:20 PM

Can you give an example of a join? My subquery needs to join on an id from the parent query and I can’t get it to work.

$sub $this->subquery->start_subquery('join''inner''t.ID = parent.ID');
$sub->select('ID')->from('table')->where('row'3);
$this->subquery->end_subquery('t'); 

Does this help?  If you show me your SQL query, I can make the code for you.

Profile
 
 
Posted: 20 February 2011 10:06 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  1
Joined  02-20-2011

Hi, I have tried using your subquery library

It seems like $sub->where_in() could not work for my subquery.
I did a $this->db->_compile_select() to print the sql statement out and I realise that there’s an addition character ` near IN condition.
Any idea how can it be solved?

Below is a subset of my statement.
JOIN `join_user_wall_post` ON `wall_post`.`wall_post_id` = `join_user_wall_post`.`wall_post_id` WHERE `join_user_wall_post`.`involved_uid` IN (‘48’, `‘102’)`

Thanks!

Profile
 
 
Posted: 20 February 2011 06:54 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  24
Joined  12-26-2008
bento85 - 20 February 2011 03:06 PM

Hi, I have tried using your subquery library

It seems like $sub->where_in() could not work for my subquery.
I did a $this->db->_compile_select() to print the sql statement out and I realise that there’s an addition character ` near IN condition.
Any idea how can it be solved?

This was happening because CodeIgniter was trying to auto-escape the main SELECT query.  I made fix for this in the library.  Download the newest version of the library and try again.

The library is located here: https://github.com/NTICompass/CodeIgniter-Subqueries

Profile
 
 
Posted: 04 July 2011 12:38 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  4
Joined  07-04-2011

hi. i found this thread while trying to figure out how to preform a subquery call properly in CI

I downloaded the library php file from GitHub and uploaded it to my server (i put in the system library folder), i am also calling the library autoload within the autoload.php file, but I am a little bit confused with the the call. 

this is the call I am trying to convert to CI


original call..

$query = sprintf(“SELECT id, address, dispensary, username, lat, lng, delivery, credit, legal, ( 3959 * acos( cos( radians(’%s’) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(’%s’) ) + sin( radians(’%s’) ) * sin( radians( lat ) ) ) ) AS distance FROM admins WHERE active=‘1’ HAVING distance < ‘%s’ ORDER BY distance LIMIT 0 , 20”,
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($center_lng),
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($radius));



what i came up with that don’t seem to be working.


function my_function_call($center_lat, $center_lng, $radius)
{
$distance = ( 3959 * acos( cos( radians(mysql_real_escape_string($center_lat)) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(mysql_real_escape_string($center_lng)) ) + sin( radians(mysql_real_escape_string($center_lat)) ) * sin( radians( lat ) ) ) );
     
$this->db->select(’*’)->from(‘admins’)->where(‘active’, 1);
$sub = $this->subquery->start_subquery(‘select’);
$sub->select(’*’)->from(‘admins’)->where(‘distance’, $distance < mysql_real_escape_string($radius));
$this->subquery->end_subquery(‘distance’);
$query = $this->results();
if($query->num_rows() > 0) return $query;
}

any help would be greatly appreciated.

Profile
 
 
Posted: 04 July 2011 02:47 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Avatar
Total Posts:  24
Joined  12-26-2008

iBeSavvvy, there are a couple things that are not quite right with your code.

$distance will not work correctly here.  The variable is not a string, so PHP will try to calculate it and store a number into the variable.  To make the variable do what you want (and also use ‘lat’ and ‘lng’ as field names) you need to make $distance a string, like so:

$center_lat $this->db->escape($center_lat);
$center_lng $this->db->escape($center_lng);

$distance "(3959*acos(cos(radians($center_lat))*cos(radians(`lat`))*cos(radians(`lng`)-radians($center_lng) )+sin(radians($center_lat))*sin(radians(`lat`))))"

“->where(‘distance’, $distance < $this->db->escape($radius))” will also not not work quite right.  PHP will try to calculate “$distance < $this->db->escape($radius)”, which will return true or false.  That would make the statement “WHERE distance = 1” or “WHERE distance = 0”.  Instead you want to do something like this:

$radius $this->db->escape($radius);
$sub->where("$distance < $radius"); 

To actually execute this query, you need to call “$this->db->get()”, and to get results, you need to use “$this->db->result()”.  Please have a look at the CodeIgniter user guide.

Also, using a subquery here in the first place is not correct.  Your current code will generate a query like this (which I don’t think is what you want, or would even work):

SELECT *, (SELECT FROM admins WHERE (3959*acos(cos(radians(...))) < 12) AS distance
FROM admins
WHERE active 

You can re-create your original query using just CodeIgniter’s Active Records (no subqueries).

$center_lat $this->db->escape($center_lat);
$center_lng $this->db->escape($center_lng);

$this->db->select('id, address, dispensary, username, lat, lng, delivery, credit, legal');
$this->db->select("(3959*acos(cos(radians($center_lat))*cos(radians(lat))*cos(radians(lng)-radians($center_lng))+sin(radians($center_lat))*sin(radians(lat)))) AS distance"false);
$this->db->from('admins')->where('active'1);
$this->db->having('distance <'$radius);
$this->db->order_by('distance');
$this->db->limit(20);
$query $this->db->get();
if(
$query->num_rows() > 0){
  
return $query->result();
}
return FALSE
Profile
 
 
Posted: 04 July 2011 04:09 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  4
Joined  07-04-2011

thanks a bunch, going to give it a shot now smile

Profile
 
 
Posted: 04 July 2011 04:26 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  4
Joined  07-04-2011

worked like a charm…thanks a lot. was over complicating it as usual.

Profile
 
 
Posted: 04 July 2011 12:16 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Avatar
Total Posts:  24
Joined  12-26-2008

You’re welcome. grin

Profile
 
 
Posted: 14 July 2011 02:39 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  7
Joined  07-14-2011

Could you help me with this query?

SELECT t.thn_ajarancon
FROM tahun_ajaran t
LEFT OUTER JOIN 
(
    
SELECT j.thn_ajaran,COUNT(*) con
    FROM siswa_tagihan st
    JOIN jenis_tagihan j ON st
.id_jenis_tagihan j.id_jenis_tagihan
    WHERE st
.nis 113080130
    
AND j.nama_tagihan 'qurban'
) as 
ON t
.thn_ajaran c.thn_ajaran 

How to rewrite it using CI Active Record Class + Subquery library that you provided?
Thx.. smile

Profile
 
 
Posted: 14 July 2011 02:54 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Avatar
Total Posts:  24
Joined  12-26-2008
azheem - 14 July 2011 06:39 PM
SELECT t.thn_ajarancon
FROM tahun_ajaran t
LEFT OUTER JOIN 
(
    
SELECT j.thn_ajaran,COUNT(*) con
    FROM siswa_tagihan st
    JOIN jenis_tagihan j ON st
.id_jenis_tagihan j.id_jenis_tagihan
    WHERE st
.nis 113080130
    
AND j.nama_tagihan 'qurban'
) as 
ON t
.thn_ajaran c.thn_ajaran 
$this->db->select('t.thn_ajaran, con');
$this->db->from('tahun_ajaran AS t');
$sub $this->subquery->start_subquery('join''left outer''t.thn_ajaran=c.thn_ajaran');
$sub->select('j.thn_ajaran')->select('COUNT(*) AS con'false);
$sub->from('siswa_tagihan AS st');
$sub->join('jenis_tagihan AS j''st.id_jenis_tagihan=j.id_jenis_tagihan');
$sub->where('st.nis'113080130)->where('j.nama_tagihan''qurban');
$this->subquery->end_subquery('c'); 
Profile
 
 
Posted: 14 July 2011 03:14 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  7
Joined  07-14-2011

Thx man.. u rock!! cool grin
I kinda stuck there coz u didn’t put the example for JOIN query in ur readme file. Maybe u should put it now? wink

Profile
 
 
Posted: 14 July 2011 03:18 PM   [ Ignore ]   [ # 13 ]  
Summer Student
Avatar
Total Posts:  24
Joined  12-26-2008

You’re welcome, dude.

When I get time, I’ll add a JOIN example to the README.  tongue laugh

Profile
 
 
Posted: 26 September 2011 08:24 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  7
Joined  07-14-2011

Recently i’ve updated my CI Reactor to version 2.0.3 and also use your latest Subquery library and i get this error:

Fatal error: Call to protected method CI_DB_active_record::_compile_select() from context ‘Subquery’

How to fix this?

Profile
 
 
Posted: 26 September 2011 11:24 PM   [ Ignore ]   [ # 15 ]  
Summer Student
Avatar
Total Posts:  24
Joined  12-26-2008
azheem - 26 September 2011 08:24 PM

Recently i’ve updated my CI Reactor to version 2.0.3 and also use your latest Subquery library and i get this error:

Fatal error: Call to protected method CI_DB_active_record::_compile_select() from context ‘Subquery’

How to fix this?

It seems they finally made _compile_select protected.  It was undocumented.  Anyway, there is an issue for CodeIgniter to add a new method.
https://github.com/EllisLab/CodeIgniter/issues/276

Once it’s added to CodeIgniter (https://github.com/EllisLab/CodeIgniter/pull/307), then this will work again.

Please follow the issue posted at GitHub: https://github.com/NTICompass/CodeIgniter-Subqueries/issues/2

I’ve updated the library to use the methods requested in the pull request.

Profile
 
 
   
1 of 2
1