注: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) {
    //
}