node 分頁查詢時,如何獲取總數和分頁資料
阿新 • • 發佈:2021-08-05
簡單程式碼
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) })
新手 如果不對可以指出