Mysql查询列中的逗号分隔值和搜索关键字中的aslo

时间:2022-06-14 00:19:35

I have one condition in query where I want to search results. In database there is region column in which region is saved as comma separated. And user select multiple regions which I get in comma separated format. How can I compare column value and search values. I want query in codeigniter.

我在查询中有一个条件,我想搜索结果。在数据库中有区域列,其中区域以逗号分隔保存。并且用户选择以逗号分隔格式获得的多个区域。如何比较列值和搜索值。我想在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();

2 个解决方案

#1


1  

I just try to answer your problem. You can use explode()

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

#2


0  

as neodan suggested in the comments you can use find in set

正如neodan在评论中建议的那样你可以在set中使用find

something like that should do the job

类似的东西应该做的工作

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

#1


1  

I just try to answer your problem. You can use explode()

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

#2


0  

as neodan suggested in the comments you can use find in set

正如neodan在评论中建议的那样你可以在set中使用find

something like that should do the job

类似的东西应该做的工作

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