laravel 查询构建器

$query=DB::connection($conn)->table($table)
->select('*', DB::raw("CONCAT(date,' ',time) AS dttm")) 
->where('P', $p) 
->whereIn('status', $status) 
->whereRaw("CONCAT(date,' ',time) between '{$start}' and '{$end}'") 
->whereBetween('Click_time', [$startTime, $endTime])
->where('id','<',6)
->orderBy('dttm', 'asc')
->orderBy('a', 'asc')
->limit(8);

$query2=DB::connection($conn)->table($table)
->select('*', DB::raw("CONCAT(date,' ',time) AS dttm")) 
->whereJsonContains('options->languages', 'en')
->whereExists(function ($query) {
  $query->select(DB::raw(1)
    ->from('orders')
    ->whereRaw('orders.user_id = users.id');
})
->orWhere(function ($query) {
  $query->where('votes', '>', 100)
  ->where('title', '<>', 'Admin');
})
->whereColumn('first_name', 'last_name')
->whereColumn('updated_at', '>', 'created_at')
->whereColumn([
          ['first_name', '=', 'last_name'],
      ['updated_at', '>', 'created_at']
])
 ->whereYear('created_at', '2017')
->whereTime('created_at', '=', '11:20')
->whereDate('created_at', '2016-10-10')
 ->whereMonth('created_at', '10')
->whereDay('created_at', '10')
->whereNotBetween('votes', [1, 100])
->whereNull('updated_at')
->whereNotNull('updated_at') 
->where('name', 'like', 'T%')
->groupBy('account_id')
->having('account_id', '>', 100)
->offset(10)
->limit(5);


$res=$query->union($query2)->get()->toArray();
$res1=$query->first();  //第一条数据,返回数据格式是StdClass对象
$res1=$query->value('dttm');  //返回第一条数据指定列的值
$res2=$query->get()->all();  //符合查询条件的所有数据,返回格式是元素为StdClass对象的Collection集合
$res2=$query->get()->toArray(); //符合查询条件的所有数据,返回格式是元素为StdClass对象的数组
$titles = DB::table('roles')->pluck('title');    //包含单个列值的数组


$roles = DB::table('roles')->pluck('title', 'name');  //返回数组中为列值指定自定义键
foreach ($roles as $name => $title) {
  echo $title;
}

 

posted @ 2022-11-19 10:47  carol2014  阅读(24)  评论(0编辑  收藏  举报