繁体   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