tp5怎么使用find_in_set?thinkphp5中find_in_set的使用方法。

admin 阅读:78 2024-03-02

最新解决方案:

采用官方建议方法:字符串条件查询

对于一些实在复杂的查询,比如find_in_set,也可以直接使用原生SQL语句进行查询,例如:

Db::table('think_user')
    ->where('find_in_set(1,sids)')
    ->select();

为了安全起见,我们可以对字符串查询条件使用参数绑定,例如:

Db::table('think_user')
    ->where('find_in_set(:id,sids)',['id'=>$id])
    ->select();

其他解决方案:

在thinkphp5中使用mysql find_in_set语法时,可以使用EXP(表达式查询)来实现,具体代码如下:

$data = Db::table('students')->where('exp','FIND_IN_SET(1,sids)')->select();

如果用数组条件查询形式的话,那么像下面这么写查询条件即可:

$where[]=['exp','FIND_IN_SET(2,sids)'];
声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!