2016-08-03 83 views
1

我在codeigniter中遇到了很大的麻煩。如何在codeigniter中編寫mysql函數join()方法

我想在codeigniter的join()函數中使用FIND_IN_SET mysql函數。但問題是codeigniter將FIND_IN_SET視爲字段名稱。

請檢查下面的代碼:

$this->db->select("gcpo.promotional_offer_id,gcpo.promotional_offer_name,gcpo.promotional_offer_code,gcpo.promotional_offer_type,gcpo.promotional_offer_discount,gcpo.promotional_offer_min_amount,gcpo.promotional_offer_uses_per_offer,gcpo.promotional_offer_start_date,gcpo.promotional_offer_end_date,name,gcpo.promotional_offer_is_active,gcpo.promotional_offer_added_date,count(gcopo.promotional_offer_code) as cntP"); 
    $this->db->from("promotional_offer gcpo"); 
    $this->db->join("customer_groups", "FIND_IN_SET(id,promotional_offer_customer_group) > 0"); 
    $this->db->join("order_promotional_offer gcopo", "gcopo.promotional_offer_code=gcpo.promotional_offer_code","left"); 
    $this->db->group_by('gcpo.promotional_offer_code'); 
    $this->db->limit($_GET['iDisplayLength'], $start); 
    $this->db->order_by($sort_array[$_GET['iSortCol_0']], $_GET['sSortDir_0']); 
    $query = $this->db->get(); 

在MySQL查詢輸出,通過笨給出:

SELECT `gcpo`.`promotional_offer_id`, `gcpo`.`promotional_offer_name`, `gcpo`.`promotional_offer_code`, `gcpo`.`promotional_offer_type`, `gcpo`.`promotional_offer_discount`, `gcpo`.`promotional_offer_min_amount`, `gcpo`.`promotional_offer_uses_per_offer`, `gcpo`.`promotional_offer_start_date`, `gcpo`.`promotional_offer_end_date`, `name`, `gcpo`.`promotional_offer_is_active`, `gcpo`.`promotional_offer_added_date`, count(gcopo.promotional_offer_code) as cntP FROM (`gc_promotional_offer` gcpo) JOIN `gc_customer_groups` ON `FIND_IN_SET`(`id,promotional_offer_customer_group)` > 0 LEFT JOIN `gc_order_promotional_offer` gcopo ON `gcopo`.`promotional_offer_code`=`gcpo`.`promotional_offer_code` GROUP BY `gcpo`.`promotional_offer_code` ORDER BY `gcpo`.`promotional_offer_added_date` desc LIMIT 10 

現在請找出MySQL查詢的FIND_IN_SET功能,你會發現像字段名,通過考慮codeigniter。

回答

2

FIND_IN_SET是一個限制功能,你想用它與where。首先,您需要加入customer_groups,然後將結果限制爲where

更改__PUT JOIN CONDITION HERE__與您的條件和前綴FIND_IN_SET與正確的表別名。

$this->db->select("gcpo.promotional_offer_id,gcpo.promotional_offer_name,gcpo.promotional_offer_code,gcpo.promotional_offer_type,gcpo.promotional_offer_discount,gcpo.promotional_offer_min_amount,gcpo.promotional_offer_uses_per_offer,gcpo.promotional_offer_start_date,gcpo.promotional_offer_end_date,name,gcpo.promotional_offer_is_active,gcpo.promotional_offer_added_date,count(gcopo.promotional_offer_code) as cntP"); 
$this->db->from("promotional_offer gcpo"); 
$this->db->join("order_promotional_offer gcopo", "gcopo.promotional_offer_code=gcpo.promotional_offer_code","left"); 
$this->db->join("customer_groups", "__PUT JOIN CONDITION HERE__"); 
$this->db->where("FIND_IN_SET(id,promotional_offer_customer_group) > 0"); 
$this->db->group_by('gcpo.promotional_offer_code'); 
$this->db->limit($_GET['iDisplayLength'], $start); 
$this->db->order_by($sort_array[$_GET['iSortCol_0']], $_GET['sSortDir_0']); 
$query = $this->db->get(); 
+0

你好Treast, 我已經習慣FIND_IN_SET功能的加入,但不是笨,而是直接到MySQL命令提示符下交互。 但是,當我使用codeigniter然後它考慮作爲一個字段。請檢查我的quetions中的mysql查詢。 我也嘗試過在哪裏的條件,但它的工作很好,但在其他事情。 但是,謝謝你的回覆。 我會在我的項目中嘗試你的代碼。如果這能行,那麼我會讓你知道 謝謝 –