0

我有这个 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
4

3 回答 3

2

试试这个:(参考)

$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();
于 2013-08-14T13:28:49.820 回答
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();
于 2013-08-14T13:29:37.463 回答
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)');

$this->db->from('crm_hotel_price');
$this->db->join('crm_destinations','cde_id = chp_destination','LEFT'); // 加入另一个表
$this->db->group_by('chp_destination,chp_year'); // 用于字段分组
$this->db->order_by("chp_id", "asc"); // 用于排序字段值
$query = $this->db->get();
返回 $query->result_array(); // 它将返回结果
并使用这一行来查看整个 SQL 查询。
回声 $this->db->last_query();

于 2013-10-28T08:56:45.883 回答