将MySQL查询转换为CodeIgniter中的Active Record

时间:2013-08-14 13:24:22

标签: mysql codeigniter activerecord

我有这个mysql查询,我是CodeIgniter的新手。如何将此mysql查询转换为CodeIgniter的Active Record?

SELECT chp_destination, cde_name, chp_year, chp_from, chp_to, chp_budget_price_high, 
chp_medium_price_high, chp_luxury_price_high, chp_budget_price_low, chp_medium_price_low, chp_luxury_price_low, chp_timestamp, chp_comment , MAX( chp_timestamp ) AS last_updated, MAX( chp_id )
FROM crm_hotel_price
LEFT JOIN crm_destinations ON cde_id = chp_destination
GROUP BY chp_destination, chp_year
ORDER BY chp_id

3 个答案:

答案 0 :(得分:2)

试试这个:(Reference)

$this->db->select('chp_destination, cde_name, chp_year, chp_from, chp_to, chp_budget_price_high, chp_medium_price_high, chp_luxury_price_high, chp_budget_price_low, chp_medium_price_low, chp_luxury_price_low, chp_timestamp, chp_comment, MAX(chp_timestamp) AS last_updated, MAX(chp_id)', FALSE);
$this->db->join('crm_destinations', 'cde_id = chp_destination', 'left');
$this->db->group_by('chp_destination, chp_year');
$this->db->order_by('chp_id');
$qry = $this->db->get('crm_hotel_price');
$res = $qry->result(); // or $qry->result_array();

答案 1 :(得分:0)

尝试:

$rs = $this->db->select('chp_destination, cde_name, chp_year, chp_from, chp_to, chp_budget_price_high, chp_medium_price_high, chp_luxury_price_high, chp_budget_price_low, chp_medium_price_low, chp_luxury_price_low, chp_timestamp, chp_comment , MAX( chp_timestamp ) AS last_updated, MAX( chp_id )', false)
      ->from('crm_hotel_price')
      ->join('crm_destinations', 'cde_id = chp_destination', 'LEFT')
      ->group_by(array('chp_destination', 'chp_year'))
      ->order_by('chp_id')
      ->get()->result_array();

答案 2 :(得分:0)

请尝试以下代码:

 > $this->db->select('chp_destination, cde_name, chp_year, chp_from, chp_to, chp_budget_price_high, 

chp_medium_price_high,chp_luxury_price_high,chp_budget_price_low,chp_medium_price_low,chp_luxury_price_low,chp_timestamp,chp_comment,MAX(chp_timestamp)AS last_updated,MAX(chp_id)');

  

$这 - > DB-肽从( 'crm_hotel_price');
  $ this-> db-> join('crm_destinations','cde_id = chp_destination','LEFT'); //加入另一张桌子
  $这 - > DB-> GROUP_BY( 'chp_destination,chp_year'); //用于分组字段
  $ this-> db-> order_by(“chp_id”,“asc”); //用于排序字段值
  $ query = $ this-> db-> get();
          return $ query-> result_array(); //它将返回结果
  并使用此行查看整个SQL查询     echo $ this-> db-> last_query();