2017-09-06 96 views
0

我在查詢一個條件,我要搜索的結果。 在數據庫中有區域列,其中區域保存爲逗號分隔。 和用戶選擇多個區域,我用逗號分隔的格式。 如何比較列值和搜索值。 我想在codeigniter中查詢。MySQL查詢的逗號分隔的搜索關鍵詞列和ASLO值

$search_id2 = "01,02,03"; 

$this->db->select('posting_id,short_desc, doc_last, country'); 
$this->db->from('abc'); 
$this->db->like('region', $search_id2); 
$query = $this->db->get(); 
+1

你有機會擺脫逗號分離的數據,並正常化您的數據呢?如果是的話,那就麻煩你了 –

+0

是,第一規範化數據庫。這將使編碼這些事情變得更容易。 –

+0

因爲在不同的功能,我不能做的數據庫更改使用同一列這是不可能的。 –

回答

1

我只是試着回答你的問題。 您可以使用explode()

$search_id2 = "01,02,03"; 
$search_id2 = explode(",",$search_id2) 
$this->db->select('posting_id,short_desc, doc_last, country'); 
$this->db->from('abc'); 
foreach ($search_id2 as $key) { 
    $this->db->or_like('region', $key); 
} 
$query = $this->db->get(); 
+0

實際上是行不通的 - 因爲如果他有區碼在他的分貝01,02,03,003,004,005和你正在尋找「01,02,03」你還用那些匹配003 – sintakonte

0

爲neodan的意見建議你可以使用find in set

這樣的事情應該做的工作

$search_id2 = "01,02,03"; 
$arrSearchIds = explode(",",$search_id2); 

$this->db 
    ->select('posting_id,short_desc, doc_last, country') 
    ->from('abc') 
    ->group_start(); 

if (count($arrSearchIds) > 0) 
{ 
    foreach($arrSearchIds AS $id) 
    { 
     $this->db->or_where("find_in_set(".$id.", region)", NULL, false); 
    } 
} 
$this->db->group_end(); 
$query = $this->db->get();