laravel 常用SQL语句


一.laravel中的sql语句集锦
1.插入数据
(1) DB::table('user')->insert($data);
(2)插入数据同时返回自增id
DB::table('user')->insertGetId($data);
2.修改
DB::table('user')->where('id',1)->update($data);
3.删除
DB::table('user')->where('id',1)->delete();
4.查找一条数据
  $res = AdminuserModel::where('id',$id)->first();
  $res = AdminuserModel::find($id);//查询id=1的数据
  $pay_data=Db::table('config')->find(1);
5.获取所有数据
  PriceModel::get();获取price表中所有数据
6.where  or .Where Between;Where Not Between
(1)where
 $users = DB::table('users')->where('votes', '>', 100)->get();
(2)or
$users = DB::table('users')->where('votes', '>', 100)->orWhere('name', 'John')->get();
(3)Where Between
$users = DB::table('users')->whereBetween('votes', array(1, 100))->get();
(4)Where Not Between
$users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();
(5)Where In
$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();
(6)Order By, Group By, And Having
$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();
(7)Offset & Limit

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

7.laravel数据库去除重复字段的内容(distinct)

      $da=WlModel::groupby('name')->distinct()->get()->toarray();
//        $da = DB::table('wl')->groupby('name')->distinct()->get();

二、连接
Joins


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


Basic Join Statement


复制代码代码如下:


DB::table('users')
  ->join('contacts', 'users.id', '=', 'contacts.user_id')
  ->join('orders', 'users.id', '=', 'orders.user_id')
  ->select('users.id', 'contacts.phone', 'orders.price')
  ->get();
左连接语句


复制代码代码如下:


DB::table('users')
  ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
  ->get();
  DB::table('users')
  ->join('contacts', function($join)
  {
  $join->on('users.id', '=', 'contacts.user_id')->orOn(...);
  })
  ->get();
  DB::table('users')
  ->join('contacts', function($join)
  {
  $join->on('users.id', '=', 'contacts.user_id')
  ->where('contacts.user_id', '>', 5);
  })
  ->get();
三、request相关用法
    1.取到单个值  $request->name;同 $request->input('name');


    $request->input('name', 'Linda'); 没有获取到用默认值


    2.取到所有值  $request->all();


    3.只取哪些值  $request->only(['age','name']);


    4.取出除了这些键的值 $request->except('name');
四、路由
  //后台登录和退出
  Route::any('/login','AdminController@login')->name('login');
  Route::any('/loginout','AdminController@loginout')->name('loginout');
  Route::group(['middleware' => ['login']], function () {//后台登录中间件
  Route::any('/', 'AdminController@index')->name('index');
  //用户管理
  Route::any('/admin/user/index','AdminuserController@index');
  });
  // 匹配[]里面的路由信息
  Route::match(['get', 'post','put'], '/getPost', function () {
      return 'Hello World';
  });
  //匹配任何一个
  Route::any('foo', function () {
      return 'Hello World1';
  });
五、分组
  有时候,您可能需要创建更高级的where子句,如“存在”或嵌套参数分组。Laravel query builder可以处理这些:
复制代码代码如下:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();


  上面的查询将产生以下SQL:
复制代码代码如下:


  select * from users where name = 'John' or (votes > 100 and title
<> 'Admin')
  Exists Statements
  DB::table('users')
  ->whereExists(function($query)
  {
  $query->select(DB::raw(1))
  ->from('orders')
  ->whereRaw('orders.user_id = users.id');
  })
  ->get();
上面的查询将产生以下SQL:


复制代码代码如下:


select * from userswhere exists (
select 1 from orders where orders.user_id = users.id
)
六、聚合


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


Using Aggregate Methods


复制代码代码如下:


$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')->min('price');
$price = DB::table('orders')->avg('price');
$total = DB::table('users')->sum('votes');
Raw Expressions


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


Using A Raw Expression


复制代码代码如下:


$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
递增或递减一个列的值


复制代码代码如下:


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



猜你喜欢

转载自blog.csdn.net/ouxiaoxian/article/details/79847784