注:laravel 查询返回的都是 php 的 stdClass 对象实例,不是数组!!!!

1)查询多行(get)

DB::table('table_name')->get();

带偏移和限制的查询(skip take 或 offset limit)(两种用法是一样的)

//skip take
DB::table('table_name')->skip(10)->take(10)->get(); //offset limit
DB::table('table_name')->offset(20)->limit(10)->get();

2)查询一行(first)

DB::table('table_name')->first();
DB::table('table_name')->find(1);

PS:find方法也可以查多行,它可以这样玩 DB::table('table_name')->find([1,2,3,4]);

3)直接查询一个字段(value)

DB::table('table_name')->where('name','Tiac')->value('email');

4)查询一(pluck)

DB::table('table_name')->where('brand_id','100')->pluck('goods_id');

5)块组结果集(chunk)

使用情景:

假设我们需要查询 1 百万的数据,并对其做处理,一次性查询 1 百万并统一处理势必会对数据产生不小的压力,消耗大量的服务器资源,有没有一种更做优的处理方法?

将 1 百万的查询分成 1000 次 1000 条记录的查询的块查询怎样?? 这个 chunk的作用,chunk方法接口一个闭包函数做回调处理

DB::table('users')->orderBy('id')->chunk(1000, function($users) {
foreach ($users as $user) {
//
}
});

PS:上面只是举例,chunk 貌似不能和 limit 一起用,chunk 默认情况下一次性处理整张表的数据,不过我可以通过自己加判断用 return false 跳出 chunk 操作

6)聚合函数(count max min avg sum 等等)

DB::table('table_name')->count();

PS:其他聚合函数的使用方法一样的,不一一举例了

7)where 条件字句

这个比较多,懒得写了,想看的直接到 laravel 学院看吧,传送门:http://laravelacademy.org/post/6140.html#ipt_kb_toc_6140_8

(where, orWhere, whereNUll, whereIn, whereNotIn, whereBetween, whereNotBetween, whereDate, whereYear, whereMonth, whereDay 等等)

8)orderBy 排序字句

DB::table('table_name')->orderBy('id','desc')->get();

PS:如果想要返回随机的查询结果集,可以使用 inRandomOrder 子句

9)groupBy 分组字句及 having havingRaw 条件子句

DB::table('table_name')->select('goods_id')->groupBy('brand_id')->having('price','>','100')->get();
DB::table('table_name')->select('brand_id')->groupBy('brand_id')->havingRaw('sum(price)','>','10000');

10)使用原生sql表达式(DB::raw 方法)

举个粟子:如果上面第9)点,我们想查询出一个销售额大于10000的品牌及销售额,我们可以这样子写:

DB::table('table_name')->select('brand_id',DB::raw('sum(price) as sales_price'))->groupBy('brand_id')->having('sales_price','>','10000');

11)join 连接查询子句

$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();

更多用法,看这里:http://laravelacademy.org/post/6140.html#ipt_kb_toc_6140_6

12)insert 插入操作

DB::table('users')->insert(
['email' => 'john@example.com', 'votes' => 0]
);

插入多行记录:

DB::table('users')->insert([
['email' => 'taylor@example.com', 'votes' => 0],
['email' => 'dayle@example.com', 'votes' => 0]
])

插入成功后返回自增id(使用insertGetId方法)

$id = DB::table('users')->insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);

13)update 更新操作

DB::table('users')->where('id', 1)->update(['votes' => 1]);

增、减操作

DB::table('users')->increment('votes', 2);
DB::table('users')->decrement('votes', 1);

increment 和 decrement 方法还有update方法的功能,第三个参数直接写要更新的数据的数组就可以了

DB::table('users')->increment('votes', 1, ['name' => 'Tiac']);

14)删除、清空操作(delete truncate)

DB::table('users')->where('votes', '>', 100)->delete();

DB::table('users')->truncate();

15)使用游标(cursor)

有自己写个框架的同学应该都知道,像 larvel 的 get 方法之类的操作,其实就是先查询获得一个查询结果集(资源),再fetch资源,组装返回一个数组,也就是说它已经先循环了一遍了,你拿到数据后,要处理的话往往又要循环一次数组;

如果查询的数据较多,从效率、性能上考虑,我们可以省略这预先一次的循环(使用游标)

foreach (DB::table('table_name')->cursor() as $row) {
//
}

大概这个样子了,更详情的说明,看这里:http://laravelacademy.org/post/6140.html

laravel 查询构建器(连贯操作)的更多相关文章

  1. [转]Laravel 数据库实例教程 —— 使用查询构建器实现对数据库的高级查询

    本文转自:https://laravelacademy.org/post/920.html 上一节我们简单介绍了如何使用查询构建器对数据库进行基本的增删改查操作,这一节我们来探讨如何使用查询构建器实现 ...

  2. Laravel 数据库实例教程 —— 使用查询构建器对数据库进行增删改查

    原文地址:https://blog.csdn.net/lmy_love_/article/details/72832259 获取查询构建器很简单,还是要依赖DB门面,我们使用DB门面的table方法, ...

  3. Laravel5.1 数据库-查询构建器

    今儿个咱说说查询构建器.它比运行原生SQL要简单些,它的操作面儿也是比较广泛的. 1 查询结果 先来看看它的语法: public function getSelect() { $result = DB ...

  4. laravel5.6操作数据curd写法(查询构建器)

    laravel5.6 数据库操作-查询构建器 <?php //laravel5.6 语法 demo示例 namespace App\Http\Controllers;//命名该控制App空间下名 ...

  5. Yii2 查询构建器 QueryBuilder

    查询构造器 QueryBuilder 1.什么是查询构建器 查询构建器也是建立在 DAO 基础之上,可让你创建程序化的.DBMS 无关的 sql 语句,并且这样创建的 sql 语句比原生的 sql 语 ...

  6. DB门面,查询构建器,Eloquent ORM三者的CURD

    一.DB门面 1.insert DB::insert('insert into table(`name`) value(?)', ['test']); 2.update DB::update('upd ...

  7. laravel增删改查(查询构建器)

    1.增 $data = [ 'username' => 'xiaohong', 'nickname' => '小红红', 'email' => '12356788@qq.com', ...

  8. 定义查询构建器IFeatureLayerDefinition

    在宗地出图,需要实现,只显示某一户人的地块.在ArcMap里,有个定义查询,可只显示过滤后的要素. 在代码中,也比较好实现,使用IFeatureLayerDefinition接口即可. IFeatur ...

  9. yii2 查询构建器

    Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...

随机推荐

  1. LeetCode: Surrounded Regions 解题报告

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  2. 由sqlite在手机上的存储位置,引发的onCreate在哪里执行的小结

    我们都知道,android为了操作数据库,一般是继承SQLiteOpenHelper类,并实现他的三个函数. 如下所示: package jz.his.db; import android.conte ...

  3. #include <algorithm>中sort的一般用法

    1.sort函数的时间复杂度为n*log2(n),执行效率较高. 2.sort函数的形式为sort(first,end,method)//其中第三个参数可选. 3.若为两个参数,则sort的排序默认是 ...

  4. java执行shell/cmd命令

    try { Process p =Runtime.getRuntime().exec("chmod 777 /home/bomb/MoveToy/WebRoot/a.sh " ); ...

  5. ASP.NET(C#)不提示直接关闭当前页面

    protected void Button1_Click(object sender, EventArgs e) { //关闭页面--要弹出提示(IE6及以下不弹出提示) ClientScript.R ...

  6. 【Android实战】----从Retrofit源代码分析到Java网络编程以及HTTP权威指南想到的

    一.简单介绍 接上一篇[Android实战]----基于Retrofit实现多图片/文件.图文上传中曾说非常想搞明确为什么Retrofit那么屌. 近期也看了一些其源代码分析的文章以及亲自查看了源代码 ...

  7. Linux 下 tail 命令

    简述 tail命令从指定点开始将文件写到标准输出,使用tail命令的“-f”选项可以方便的查阅正在改变的日志文件,“tail -f filename”会把filename里最尾部的内容显示在屏幕上,并 ...

  8. 解决 Plugin with id 'com.github.dcendents.android-maven' not found.

    在Android studio中引用第三方库的时候,报这个错. Error:(2, 0) Plugin with id 'com.github.dcendents.android-maven' not ...

  9. 【Unity笔记】将角色的碰撞体朝向鼠标点击方向——角色朝向鼠标

    int floorMask; // 自动寻路层 void Awake() { floorMask = LayerMask.NameToLayer("Floor"); } void ...

  10. Linux远程复制文件

    将本机文件app.properties 复制到用户为root,ip为ip的具体路径下去 scp app.properties root@ip:/apps/javaconf/common/ 其他参考: ...