簡體   English   中英

MySQL查詢以逗號分隔的列中的值和搜索關鍵字中的aslo

[英]Mysql query for Comma separated values in columns and aslo in search keywords

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

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

我只是想回答你的問題。 您可以使用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();

正如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();

暫無
暫無

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

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