A-A+

Laravel SQL 查询语句集锦

2021年12月26日 PHP开源系统 暂无评论 阅读 0 views 次

1、从数据表中取得单一数据列

$user= DB::table('users')->where('name','John')->first();

2、检索表中的所有行,代码如下:

  1. $users = DB::table('users')->get();  
  2. foreach ($users as $user)  
  3. {  
  4. var_dump($user->name);  
  5. }  

3、从表检索单个行,代码如下:

$user = DB::table('users')->where('name', 'John')->first();
var_dump($user->name);

检索单个列的行,代码如下:

$name = DB::table('users')->where('name', 'John')->pluck('name');

检索一个列值列表,代码如下:

$roles = DB::table('roles')->lists('title');

该方法将返回一个数组标题的作用。你也可以指定一个自定义的键列返回的数组,代码如下:

$roles = DB::table('roles')->lists('title', 'name');

指定一个Select子句,代码如下:

  1. $users = DB::table('users')->select('name', 'email')->get();  
  2.  $users = DB::table('users')->distinct()->get();  
  3.  $users = DB::table('users')->select('name as user_name')->get();  

Select子句添加到一个现有的查询

$query = DB::table('users')->select('name');

$users = $query->addSelect('age')->get();

where

$users = DB::table('users')->where('votes', '>', 100)->get();

OR

$users = DB::table('users')->where('votes', '>', 100)->orWhere('name', 'John')->get();

Where Between

$users = DB::table('users')->whereBetween('votes', array(1, 100))->get();

Where Not Between

$users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();

Where In With An Array

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();

$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();

Using Where Null To Find Records With Unset Values

$users = DB::table('users')->whereNull('updated_at')->get();

Order By, Group By, And Having

$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();

Offset & Limit

$users = DB::table('users')->skip(10)->take(5)->get();

二、连接

Joins

查询构建器也可以用来编写连接语句,看看下面的例子:

Basic Join Statement

  1. DB::table('users')  
  2.   ->join('contacts', 'users.id', '=', 'contacts.user_id')  
  3.   ->join('orders', 'users.id', '=', 'orders.user_id')  
  4.   ->select('users.id', 'contacts.phone', 'orders.price')  
  5.   ->get();  

左连接语句,代码如下:

  1. DB::table('users')  
  2.   ->leftJoin('posts', 'users.id', '=', 'posts.user_id')  
  3.   ->get();  
  4.   DB::table('users')  
  5.   ->join('contacts', function($join)  
  6.   {  
  7.   $join->on('users.id', '=', 'contacts.user_id')->orOn(...);  
  8.   })  
  9.   ->get();  
  10.   DB::table('users')  
  11.   ->join('contacts', function($join)  
  12.   {  
  13.   $join->on('users.id', '=', 'contacts.user_id')  
  14.   ->where('contacts.user_id', '>', 5);  
  15.   })  
  16.   ->get();  

三、分组

有时候,您可能需要创建更高级的where子句,如“存在”或嵌套参数分组。Laravel query builder可以处理这些:

  1. DB::table('users')  
  2. ->where('name', '=', 'John')  
  3. ->orWhere(function($query)  
  4. {  
  5. $query->where('votes', '>', 100)  
  6. ->where('title', '<>', 'Admin');  
  7. })  
  8. ->get();  

上面的查询将产生以下SQL:

  1. select * from users where name = 'John' or (votes > 100 and title   
  2.  'Admin')  
  3. Exists Statements  
  4. DB::table('users')  
  5. ->whereExists(function($query)  
  6. {  
  7. $query->select(DB::raw(1))  
  8. ->from('orders')  
  9. ->whereRaw('orders.user_id = users.id');  
  10. })  
  11. ->get();  

上面的查询将产生以下SQL:

  1. select * from userswhere exists (  
  2. select 1 from orders where orders.user_id = users.id  
  3. )  

四、聚合

查询构建器还提供了各种聚合方法,如统计,马克斯,min,avg和总和。

Using Aggregate Methods

  1. $users = DB::table('users')->count();  
  2. $price = DB::table('orders')->max('price');  
  3. $price = DB::table('orders')->min('price');  
  4. $price = DB::table('orders')->avg('price');  
  5. $total = DB::table('users')->sum('votes');  

Raw Expressions

有时您可能需要使用一个原始表达式的查询。这些表达式将注入的查询字符串,所以小心不要创建任何SQL注入点!创建一个原始表达式,可以使用DB:rawmethod:

Using A Raw Expression 代码如下:

  1. $users = DB::table('users')  
  2. ->select(DB::raw('count(*) as user_count, status'))  
  3. ->where('status', '<>', 1)  
  4. ->groupBy('status')  
  5. ->get();  

递增或递减一个列的值,代码如下:

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

您还可以指定额外的列更新:

DB::table('users')->increment('votes', 1, array('name' => 'John'));

Inserts 将记录插入表,代码如下:

  1. DB::table('users')->insert(  
  2. array('email' => 'john@example.com', 'votes' => 0)  
  3. );  

将记录插入表自动增加的ID

如果表,有一个自动递增的id字段使用insertGetId插入一个记录和检索id,代码如下:

  1. $id = DB::table('users')->insertGetId(  
  2. array('email' => 'john@example.com', 'votes' => 0)  
  3. );  

注意:当使用PostgreSQL insertGetId方法预计,自增列被命名为“id”。

多个记录插入到表中,代码如下:

  1. DB::table('users')->insert(array(  
  2. array('email' => 'taylor@example.com', 'votes' => 0),  
  3. array('email' => 'dayle@example.com', 'votes' => 0),  
  4. ));  

四、Updates

更新一个表中的记录,代码如下:

  1. DB::table('users')  
  2. ->where('id', 1)  
  3. ->update(array('votes' => 1));  

五、Deletes

删除表中的记录,代码如下:

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

删除表中的所有记录,代码如下:

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

删除一个表,代码如下:

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

六、Unions

查询构建器还提供了一种快速的方法来“联盟”两个查询:

  1. $first = DB::table('users')->whereNull('first_name');  
  2. $users = DB::table('users')->whereNull('last_name')->union($first)->get();  

unionAll方法也可以,有相同的方法签名。

Pessimistic Locking

查询构建器包括一些“悲观锁定”功能来帮助你做你的SELECT语句,运行SELECT语句“共享锁”,你可以使用sharedLock方法查询:

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

更新“锁”在一个SELECT语句,您可以使用lockForUpdate方法查询:

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

七、缓存查询

你可以轻松地缓存查询的结果使用记忆法:

$users = DB::table('users')->remember(10)->get();

在本例中,查询的结果将为十分钟被缓存。查询结果缓存时,不会对数据库运行,结果将从默认的缓存加载驱动程序指定您的应用程序,如果您使用的是支持缓存的司机,还可以添加标签来缓存:

$users = DB::table('users')->cacheTags(array('people', 'authors'))->remember(10)->get();

学习时的痛苦是暂时的 未学到的痛苦是终生的.

标签:

给我留言