lavarel7聚合查询
//select count(*)from articles where id>5
//select count(id)from articles where id>5
$res =DB::table(‘articles’)->count();
$res =DB::table(‘articles’)->where(‘id’,’<’,5)->count();
// select sum(pv) from articles
$res = DB::table(‘articles’)->sum(‘pv’);
// select min(pv) from articles
// select min(pv) from articles where status=1 and pv>=16
$res = DB::table(‘articles’)->where(‘status’,1)->where(‘pv’,’>=’,16)->min(‘pv’);
// select max(pv) from articles
$res = DB::table(‘articles’)->max(‘pv’);
// select avg(pv) from articles
$res = DB::table(‘articles’)->avg(‘pv’);
// select from articles where status=1 and pv>0 and pv<23
// select from articles where status=1 and pv between 0 and 23
$res = DB::table(‘articles’)->where(‘status’,1)->whereBetween(‘pv’,[0,23])->get()->all();
// select from articles where pv=3 or pv=20 or pv=10
// select from articles where pv in(3,20,10)
$res = DB::table(‘articles’)->whereIn(‘pv’,[3,20,10])->get()->all();
// select * from articles where pv=3 or pv=20 or pv=10
$res = DB::table(‘articles’)->orWhere(‘pv’,3)->orWhere(‘pv’,20)->orWhere(‘pv’,10)->get()->all();
// select from articles,article_cate where articles.cate_id=article_cate.cid
$res = DB::table(‘articles’)->leftJoin(‘article_cate’,’articles.id’,’=’,’article_cate.cid’)->select(‘articles.‘,’article_cate.title as ctitle’)->get()->all();