node 分页查询时,如何获取总数和分页数据

简单代码 

  SELECT SQL_CALC_FOUND_ROWS * from staff  WHERE companyid='xxx' LIMIT 0,10;

  SELECT FOUND_ROWS() as total;

 

node JS 案例

 

let sqltxt=`SELECT SQL_CALC_FOUND_ROWS staffid,staffname,staffnum,departmen from staff WHERE companyid='xxx' LIMIT 0,10;`
let sqltxt2=`SELECT FOUND_ROWS() as total`
    const P1=new Promise((resolve,reject)=>{
      connection.query(sqltxt,function(err,data){
        if(err){
          reject(err)
        }else{
          resolve(data)
        }
      })
    })
    const P2=new Promise((resolve,reject)=>{
      connection.query(sqltxt2,function(err,data){
        if(err){
          reject(err)
        }else{
          resolve(data)
        }
      })
    })
    Promise.all([P1,P2]).then(result=>{
      res.send({ code: 200, msg: "查询成功!",data:result[0] ,total:result[1][0].total}).end(); 
    },err=>{
      console.log("err",err)
    })

 

新手 如果不对可以指出

 

  

posted @ 2021-08-05 10:35  伴月阁  阅读(1098)  评论(0编辑  收藏  举报