设为首页 加入收藏

TOP

TP5 查询mysql数据库时的find_in_set用法
2019-08-15 23:26:49 】 浏览:14
Tags:TP5 查询 mysql 数据库时 find_in_set 用法
$where['class_id'] = ['in', '$cid_all'];
$where['id'] = ['in', $all_user_id];//或这样子
$where['title'] = ['like', '%php%'];
$where['id'] = ['<>', $id];
$where['id'] = ['notin', $all_user_id];//不等于
与find_in_set()相反的函数可以用!find_in_set() //值为数字 $id=419; $where[] = ['exp',Db::raw("FIND_IN_SET($id,category)")];//category值为数字,例子:419,415,414 //值为字符串 $id值等于dfd 要注意'引号
$where[] = ['exp',Db::raw("FIND_IN_SET('$id',category)")];//category值为数字,例子:'349/417/419','349/413/415','349/413/416'
Db::name('menu')->where('FIND_IN_SET(:id,pid_all)',['id' => $id])->update([$field => $title]);

type 字段在数据库是以 1,2,3 形式存在 已说到FIND_IN_SET函数的使用

 

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

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

 

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

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

自己的实例:

$gonggaolist = Db::table("fa_cms_archives")
            ->alias('a')
            ->join("fa_cms_channel w","a.channel_id=w.id")
            ->where("w.parent_id=20 and a.status='normal'")
            ->where("FIND_IN_SET('recommend',a.flag)")
            ->order("a.id desc")
            ->field("a.id,a.channel_id,a.title,a.likes,a.image,w.diyname,a.updatetime,a.flag")
            ->limit("4")
            ->select();
        dump($gonggaolist);

效果图

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【php设计模式】装饰器模式 下一篇【php设计模式】门面模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目