简体   繁体   中英

Adding a substring in a CodeIgniter Where Clause

Is it possible to add a substring in an active query?

I have this example that works when I write it out in pure sql. However, when I write it in an active query in CI, the results don't show. I was wondering if someone can help verify if this is correct.

        $this->db->distinct();
        $this->db->select('user_table.id','user_table.first_name','user_table.last_name','user_table.email','user_table.created_on');
        $this->db->from($this->user_table);
        $this->db->join($this->account_items_table,'user_accounts.id = account_items.user_id','LEFT');
        $this->db->where('SUBSTRING(account_items.key,1,2)',$input);

CI可能在表达式周围添加反引号,并在where()中将第三个参数传递为false

$this->db->where('SUBSTRING(account_items.key,1,2)',$input,false);

This works fine for me:

$this->db->where('SUBSTRING(title, 1, 1)=','H');
$query = $this->db->get('news');
print $this->db->last_query();

The only difference I can see is I have included the = operator.

As you can see it returns the article named "Hello": https://mi-linux.wlv.ac.uk/~in9352/codeigniter3/

And the SQL statement is: SELECT * FROM news WHERE SUBSTRING(title, 1, 1) = 'H'

As suggested you should use

print $this->db->last_query();

To see what YOUR SQL is... It allowed to spot the missing = operator in my case... debugging is always useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM