【问题标题】:Adding a substring in a CodeIgniter Where Clause在 CodeIgniter Where 子句中添加子字符串
【发布时间】:2018-01-03 14:33:23
【问题描述】:

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

我有这个例子,当我用纯 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);

【问题讨论】:

  • 在您的问题中包含$this->user_column_order$this->user_table$this->account_items_table 的值
  • account_items 只使用 user_id。

标签: php mysql codeigniter


【解决方案1】:

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

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

【讨论】:

  • @JianYA 在执行活动记录的查询生成器后运行echo $this->db->last_query(); 会得到什么?
  • 感谢您的帮助。我想我会改用另一种方法。
【解决方案2】:

这对我来说很好用:

$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 是什么...在我的情况下,它允许发现缺少的 = 运算符...调试总是有用的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多