繁体   English   中英

在CodeIgniter Where子句中添加子字符串

[英]Adding a substring in a CodeIgniter Where Clause

是否可以在活动查询中添加子字符串?

当我用纯sql将其写出时,我有这个示例可以工作。 但是,当我在CI中的活动查询中编写它时,结果不会显示。 我想知道是否有人可以帮助验证这是否正确。

        $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->db->where('SUBSTRING(title, 1, 1)=','H');
$query = $this->db->get('news');
print $this->db->last_query();

我可以看到的唯一区别是我包含了=运算符。

如您所见,它将返回名为“ Hello”的文章: https : //mi-linux.wlv.ac.uk/~in9352/codeigniter3/

SQL语句为:SELECT * FROM news WHERE SUBSTRING(title,1,1)='H'

根据建议,您应该使用

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

看看您的SQL是什么...就我而言,它允许发现缺少的=运算符...调试总是很有用的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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