codeigniter 在 find_in_set 中添加 IS NULL

codeigniter adding the IS NULL in the find_in_set

通过 codeigniter 框架生成查询时

$this->db->select('id,memo,sent_to,sent_by,read_by,date')->from('memos')
    ->where("FIND_IN_SET('1',`sent_to`)")->order_by('`id`','DESC')->get();

我在查询中自动添加 IS NULL 时出错

它产生

SELECT `id`, `memo`, `sent_to`, `sent_by`, `read_by`, `date` FROM `memos` 
WHERE FIND_IN_SET('1',`sent_to`) IS NULL ORDER BY `id` DESC 

而不是

SELECT `id`, `memo`, `sent_to`, `sent_by`, `read_by`, `date` FROM `memos` 
WHERE FIND_IN_SET('1',`sent_to`) ORDER BY `id` DESC

使用这个

$query = $this->db->query("SELECT * FROM memos WHERE FIND_IN_SET('1',sent_to) ORDER BY id DESC");
$result = $query->result_array();
return $result;

result_array(); this for return data as objective array

您只需为字符串变量编写一个 mysql 查询,然后 运行 使用 $this->db->query(''); 而不是使用活动记录。

P/s:抱歉我的英语不好。

您需要添加 !=0 是您的 where clause 以删除 IS NULL

$this->db->select('id,memo,sent_to,sent_by,read_by,date')->from('memos')
    ->where("FIND_IN_SET('1',`sent_to`)!=",0)->order_by('`id`','DESC')->get();

以下代码 100% 有效。 试试看,

    $this->db->select('id,memo,sent_to,sent_by,read_by,date');
    $this->db->from('memos');
    $this->db->where("FIND_IN_SET('1',`sent_to`) !=", 0);
    $this->db->order_by('`id`','DESC');
    $this->db->get();

find_in_set() returns 数值所以这就是为什么我们需要使用关系运算符来评估条件 喜欢

$this->db->where("FIND_IN_SET('x',field_list)>0");