简体   繁体   English

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

[英]Convert MySQL query to Active Record in CodeIgniter

I have this mysql query and I am very new to CodeIgniter. 我有这个mysql查询,我对CodeIgniter很新。 How can I convert this mysql query into CodeIgniter's Active Record? 如何将此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

Try this: (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();

Try: 尝试:

$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();

Please try this code: 请尝试以下代码:

 > $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 )'); 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)');

$this->db->from('crm_hotel_price'); $这 - > DB-从( 'crm_hotel_price')>;
$this->db->join('crm_destinations','cde_id = chp_destination','LEFT'); $ this-> db-> join('crm_destinations','cde_id = chp_destination','LEFT'); // join with another table //与另一个表连接
$this->db->group_by('chp_destination,chp_year'); $这 - > DB-> GROUP_BY( 'chp_destination,chp_year'); // for grouping of field //用于分组字段
$this->db->order_by("chp_id", "asc"); $ this-> db-> order_by(“chp_id”,“asc”); // for sorting field value //用于排序字段值
$query = $this->db->get(); $ query = $ this-> db-> get();
return $query->result_array(); return $ query-> result_array(); // It will return result //它会返回结果
and use this line to view whole SQL query. 并使用此行来查看整个SQL查询。
echo $this->db->last_query(); echo $ this-> db-> last_query();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM