tp3.2 多表联合查询及多表多字段模糊检索

浏览:5596 发布日期:2017/11/10 分类:技术分享 关键字: 多字段模糊查询
本人小白一枚,今天做项目用到了多表及模糊查询。再次记录一下

检索界面

源码$User = M('finance'); // 實例化User對象
        $data_P = I('get.');
        if (IS_GET) {//搜索
            if($data_P['user_id']){//模糊检索
                $map['u.phone | u.true_name | w.money | w.content' ] =array('like',"%{$data_P['user_id']}%") ;
            }
            if($data_P['type']){//按类型搜索
                $map['type'] = $data_P['type'];
            }
            if($data_P['startime']){//只输入起始时间
                $startime = date("Y-m-d 00:00:01", strtotime($data_P['startime']));
                $map['w.createTime'] = array('egt', $startime);
            }
            if($data_P['endtime']){//只输入结束时间
                $endtime = date("Y-m-d 00:00:01", strtotime($data_P['endtime']));
                $map['w.createTime'] = array('elt', $endtime);
            }
            if ($data_P['startime'] && $data_P['endtime']) {//开始和结束时间搜索
                $startime = date("Y-m-d 00:00:01", strtotime($data_P['startime']));
                $endtime = date("Y-m-d 23:59:59", strtotime($data_P['endtime']));
                $map['w.createTime'] = array('between', array($startime, $endtime));
            }
        }
        $count = $User->alias("w")->join("LEFT JOIN gh_user as u on w.user_id = u.id")->where($map)->count(); // 查詢滿足要求的總記錄數
        $p = getpage($count, 20);
        $list = $User->alias("w")                                        //左表起别名
                     ->join("LEFT JOIN gh_user as u on w.user_id = u.id")//左链接
                     ->field("w.*,u.phone,u.true_name")                  //左表显示全部字段,右表显示部分字段
                     ->where($map)                                       //条件
                     ->order('w.createTime DESC')                        //排序
                     ->limit($p->firstRow, $p->listRows)                 //分页
                     ->select();


        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign("data",$data_P);
        $this->display('index/fiancelist');
这样查询有个缺点就是运行起来会慢一点,希望有大神指出更好的方案!
最佳答案
评论( 相关
后面还有条评论,点击查看>>