本文实例讲述了Laravel5.1 框架数据库查询构建器用法。分享给大家供大家参考,具体如下:
今儿个咱说说查询构建器。它比运行原生SQL要简单些,它的操作面儿也是比较广泛的。
1 查询结果
先来看看它的语法:
public function getSelect()
{
$result = DB::table('articles')->get();
dd($result);
}
查询构建器就是通过table方法返回的,使用get()可以返回一个结果集(array类型) 这里是返回所有的数据,当然你也可以链接很多约束。
1.1 获取一列/一行数据
public function getSelect()
{
$result = DB::table('articles')->where('title', 'learn database')->get(); // 获取整列数据
$articles = DB::table('articles')->where('title', 'learn database')->first(); // 获取一行数据
dd($result, $articles);
}
我们可以通过where来增添条件。
1.2 获取数据列值列表
如果你想要取到某列的值的话 可以使用lists方法:
public function getSelect()
{
$result = DB::table('articles')->where('id', '<', 2)->lists('title');
$titles = DB::table('articles')->lists('title');
dd($result, $titles);
}
1.3 获取组块儿结果集
在我们数据表中数据特别特别多时 可以使用组块结果集 就是一次获取一小块数据进行处理
public function getSelect()
{
DB::table('articles')->chunk(2, function ($articles){
foreach ($articles as $article){
echo $article->title;
echo "<br />";
}
});
}
如果说要终止组块运行的话 返回false就可以了:
public function getSelect()
{
DB::table('articles')->chunk(2, function ($articles){
return false;
});
}
1.4 聚合函数
构建器还提供了很多的实用方法供我们使用:
- count方法:返回构建器查询到的数据量。
- max方法:传入一列 返回这一列中最大的值。
- min方法:跟max方法类似,它返回最小的值。
- sum方法:返回一列值相加的和。
- avg方法:计算平均值。
1.4.1 count
public function getArticlesInfo()
{
$article_count = DB::table('articles')->count();
dd($article_count);
}
1.4.2 max
public function getArticlesInfo()
{
$maxCommentCount = DB::table('articles')->max('comment_count');
dd($maxCommentCount);
}
1.4.3 sum
public function getArticlesInfo()
{
$commentSum = DB::table('articles')->sum('comment_count');
}
1.4.4 avg
public function getArticlesInfo()
{
$commentAvg = DB::table('articles')->avg('comment_count');
dd($commentAvg);
}
1.5 select查询
1.5.1 自定义子句
select语句可以获取指定的列,并且可以自定义键:
public function getArticlesInfo()
{
$articles = DB::table('articles')->select('title')->get();
// 输出结果:
// array:3 [▼
// 0 => {#150 ▼
// "title": "laravel database"
// }
// 1 => {#151 ▼
// "title": "learn database"
// }
// 2 => {#152 ▼
// "title": "alex"
// }
// ]
$articles1 = DB::table('articles')->select('title as articles_title')->get();
// 输出结果:
// array:3 [▼
// 0 => {#153 ▼
// "articles_title": "laravel database"
// }
// 1 => {#154 ▼
// "articles_title": "learn database"
// }
// 2 => {#155 ▼
// "articles_title": "alex"
// }
// ]
$articles2 = DB::table('articles')->select('title as articles_title', 'id as articles_id')->get();
// array:3 [▼
// 0 => {#156 ▼
// "articles_title": "laravel database"
// "articles_id": 1
// }
// 1 => {#157 ▼
// "articles_title": "learn database"
// "articles_id": 2
// }
// 2 => {#158 ▼
// "articles_title": "alex"
// "articles_id": 3
// }
// ]
}
1.5.2 distinct方法
关于distinct方法我还没弄明白到底是什么意思 适用于什么场景,也欢迎大神们给出个答案 谢谢
distinct方法允许你强制查询返回不重复的结果集。
public function getArticlesInfo()
{
$articles = DB::table('articles')->distinct()->get();
}
1.5.3 addSelect方法
如果你想要添加一个select 可以这样做:
public function getArticlesInfo()
{
$query = DB::table('articles')->select('title as articles_title');
$articles = $query->addSelect('id')->get();
dd($articles);
}
2 where语句
where语句是比较常用的,经常用他来进行条件筛选。
2.1 where基础介绍
现在来详细介绍下where方法 它接收三个参数:
- 列名,这个没什么好说的。
- 数据库系统支持的操作符,比如说 ”=“、”<“、”like“这些,如果不传入第二个参数 那么默认就是”=“等于。
- 要比较的值。
public function getArticlesInfo()
{
$articles1 = DB::table('articles')->where('id','2')->get(); // 等于
$articles2 = DB::table('articles')->where('id','>','2')->get(); // 大于
$articles3 = DB::table('articles')->where('id','<>','2')->get(); // 不等于
$articles4 = DB::table('articles')->where('id','<=','2')->get(); // 小于等于
$articles5 = DB::table('articles')->where('title','LIKE','