zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Laravel执行原生SQL语句及使用paginate分页

SQL执行 语句 原生 分页 laravel 使用
2023-09-27 14:27:34 时间

概述

1、运行原生sql

ublic function getList($data){
//获取前端传过来的参数
  $user = $data['userId'];
  $office = $data['officeId'];
  $key = $data['oneKeySearch'];
//进行模糊搜索和联合查询
  $where = 'and 1=1 ';
  if($key!=null) {
    $where.= ' and ( a.code like "%' . $key . '%"';
    $where.= ' or b.name like "%' . $key . '%"';
    $where.= ' or c.name like "%' . $key . '%")';
  }
//对前端传回的字段进行判断,如果不为空则执行条件查询
  if($user!=null){
    $user='and a.userId='.$user;
  }
  if($office!=null){
    $office='and a.officeId='.$office;
  }
//自定义原生sql语句,%s可以传参数到sql语句中,格式如下:
  $sqlTmp=sprintf('select a.id,a.code,a.attendanceRate,a.statisticTime,
            b.`realName` as userName,c.`name` as officeName
            from xxxa1
            LEFT JOIN xxx2 b ON