簡體   English   中英

WHERE_IN僅在codeigniter中返回第一個匹配項

[英]WHERE_IN returns only first match in codeigniter

選擇復選框時,我試圖通過ajax獲取優惠券列表。 因此,其他所有工作都很好,但查詢僅返回第一個匹配項。

所以我的查詢是:

$this->db->from('tbl_coupons');
        if($storeids !=''){
         $ids = array($storeids);
$this->db->where_in('coupon_store', $ids );
        }       
        $this->db->where('coupon_cat', $catid);
        $this->db->where('coupon_status', 'active');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            $ds = $query->result_array();}

據此,我的SQLquery成為

SELECT * FROM `tbl_coupons` 
WHERE `coupon_store` IN('1,97') 
AND `coupon_cat` = '16' 
AND `coupon_status` = 'active'

但是此查詢返回的值帶有coupon_store=1而沒有結果coupon_store=97

我檢查了該類別中存在的優惠券商店97的值。

如果數據存在,將以下面的方式使用它將成為查詢的一部分。

        storeids = explode(',',storeids);
        $ids = array();
        foreach($storeids as $val){
            $ids[] = $val;
        }
        if(!empty($ids)){
            $this->db->where_in('coupon_store', $ids );
        }

希望它將創建正確的SQL查詢

該查詢大部分是正確的,除了在第2行,您需要按以下方式進行更改:

WHERE coupon_store IN('1','97')

其他一切都保持不變。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM