美文网首页PHP实战
Thinkphp 5 tp5 框架中 sql 用 or and

Thinkphp 5 tp5 框架中 sql 用 or and

作者: OSong | 来源:发表于2017-05-22 22:43 被阅读616次

    今天在该别人代码时遇到这个问题,首先在SQL查询外写好where()条件,然后SQL语句如下。 使用了匿名函数,用 use 引用匿名函数的外部变量

     SELECT * FROM `ny_information` 
    WHERE `status` = 1 
    AND (`username` LIKE '%1111%' 
    OR `cert_number` LIKE '%1111%' 
    OR `username` LIKE '%1111%') 
    ORDER BY input_time desc LIMIT 0,8
    
    $whereOr = ' ';//已经给其赋了where条件
    $where_1 = ' ';
    $where_2 = ' ';
    $info = Db::table('ny_information')
                ->where('status', '=', 1) //where      whereOr 
                  //匿名函数用法
                ->where(function($query) use ($where_1, $whereOr, $where_2){
                          $query->where($where_1)
                                    ->whereOr($whereOr)
                                    ->whereOr($where_2);
                           })
                 ->order("input_time desc")
                 ->paginate(8,false,['query' => request()->param()]);
            
    

    相关文章

      网友评论

        本文标题:Thinkphp 5 tp5 框架中 sql 用 or and

        本文链接:https://www.haomeiwen.com/subject/hqgixxtx.html