codeigniter adding the IS NULL in the find_in_set

2020-04-16 15:43发布

while generating the query through codeigniter framework

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

i got an error of adding IS NULL in the query automatically

it produce

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 

instead of

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

5条回答
淡お忘
2楼-- · 2020-04-16 15:49

You simply need to write a mysql query for string variable and run it by using $this->db->query(''); instead of using active record.

P/s: Sorry for my bad english.

查看更多
孤傲高冷的网名
3楼-- · 2020-04-16 15:55

Use this

$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

查看更多
女痞
4楼-- · 2020-04-16 15:58

find_in_set() returns numeric value so thats why we need to use relational operator to evaluate condition like

$this->db->where("FIND_IN_SET('x',field_list)>0");
查看更多
闹够了就滚
5楼-- · 2020-04-16 16:14

Below code 100% work. try it,

    $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();
查看更多
祖国的老花朵
6楼-- · 2020-04-16 16:15

You need to add !=0 is your where clause to remove 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();
查看更多
登录 后发表回答