0
votes

I'm having a problem converting below MySQL code to a Codeigniter active record query.

SELECT sss.*, c.country_name, c.country_code 
  FROM ( SELECT gr.* 
           FROM (`be_goldrate` as `gr`) 
          WHERE `gr`.`country_id` != 0 
            AND `gr`.`rate_type` = 'calculated' 
           ORDER BY gr.date DESC ) as sss 
   LEFT JOIN `be_countries` as `c` 
     ON `c`.`country_id`=`sss`.`country_id`  
   GROUP BY `sss`.`country_id`

I've tried as suggested here but without success.

1
what error do you get? - Joel Harkes
you might want to make an sql view of: SELECT gr.* FROM (be_goldrate as gr) WHERE gr.country_id != 0 AND gr.rate_type = 'calculated' ORDER BY gr.date DESC ) ? to increase performance? - Joel Harkes
@joelharkes no it is to get latest be_goldreate's data - Prakash
CodeIgniter doesn't support subqueries natively. If you want, you can try my subquery library, it should let you do what you want. github.com/NTICompass/CodeIgniter-Subqueries - Rocket Hazmat
CI 3.0 will have subquery (or "grouping") supported. - Aken Roberts

1 Answers

0
votes

you just need to check the Codeigniter Documentation

     $this->db->select('sss.*, c.country_name, c.country_code ');
       $this->db->from('(SELECT gr.* 
       FROM (`be_goldrate` as `gr`) 
      WHERE `gr`.`country_id` != 0 
        AND `gr`.`rate_type` = "calculated" 
       ORDER BY gr.date DESC) as sss');
       $this->db->join('be_countries as c','c.country_id = sss.country_id','left');

       $this->db->group_by('sss.country_id'); 

please report if any error