laravel6.x数据库增删改查操作【查询构造器篇】

Laravel 能使用原生 SQL、流畅的查询构造器Eloquent ORM 在各种数据库后台与数据库进行非常简单的交互。

laravel6.x数据库增删改查操作【原生 SQL篇】

一,常用查询

(1)一般查询

// 获取全部数据
$tests = DB::table('test')->get();

// 获取全部数据(不重复)
$tests = DB::table('test')->distinct()->get();

// 获取全部数据(指定字段)
$tests = DB::table('test')->select('id', 'name as name1')->get();

// 获取全部数据之orderBy
$tests = DB::table('test')->orderBy('id', 'desc')->get();

// 获取全部数据之根据时间排序
// latest是最新时间排序,反之oldest()。如果不写参数的话,默认字段为created_at。
$tests = DB::table('test')->latest('dateTimeTest')->get();

// 随机取一行数据
$tests = DB::table('test')->inRandomOrder()->first();

// 获取单行
$test = DB::table('test')->get()->first();

// 获取单行(指定where条件)
$test = DB::table('test')->where('name', 'test')->first();

// 从第几个开始取几个(第一种)
$tests = DB::table('test')->skip(1)->take(5)->get();

// 从第几个开始取几个(第二种)
$tests = DB::table('test')->offset(1)->limit(5)->get();

// 获取单行的单个值
$age = DB::table('test')->where('name', 'test')->value('age');

// 获取一列的值
$names = DB::table('test')->pluck('name');
foreach ($names as $name) {
	echo $name;
}

(2)条件查询

// 条件语句 if
$str = "2";
// $str为true时执行闭包
$tests = DB::table('test')->when($str, function ($query, $str) {
	return $query->where('id', $str);
})->get();

// 条件语句 if else
$str = 1;
$tests = DB::table('test')->when($str, function ($query, $str) {
	// $str为true时执行
	return $query->orderBy('name','desc');
}, function ($query) {
	// $str为false时执行
	return $query->orderBy('name');
})->get();

(3)where查询

$tests = DB::table('test')->where('name','=','ddddd')->get();

$tests = DB::table('test')->where('name','like','d%')->get();

$tests = DB::table('test')->where([
	['age', '=', '22'],
	['name', '<>', 'ddddd'],
])->get();

// or
$tests = DB::table('test')->where('age', '>', 10)->orWhere('name', 'ddddd')->get();

$tests = DB::table('test')->whereBetween('age', [1, 100])->get();

$tests = DB::table('test')->whereBetween('age', [1, 100])->orWhereBetween('age',[100,200])->get();
//whereNotBetween和orWhereNotBetween省略

$tests = DB::table('test')->whereIn('id', [1, 2, 3])->get();
//whereNotIn 省略

$tests = DB::table('test')->whereNull('age')->get();// whereNull 只查找age为null的
// whereNotNull省略

//whereDate 比较字段和给定日期的关系,数据表里有一条dateTimeTest值为2019-10-09 12:45:11
$tests = DB::table('test')->whereDate('dateTimeTest', '2019-10-09')->get();

$tests = DB::table('test')->whereMonth('dateTimeTest', '10')->get();

$tests = DB::table('test')->whereDay('dateTimeTest', '09')->get();

$tests = DB::table('test')->whereYear('dateTimeTest', '2019')->get();

$tests = DB::table('test')->whereTime('dateTimeTest', '=','12:45:11')->get();

// 取出两个字段的值相等的结果
$tests = DB::table('test')->whereColumn('age', 'weight')->get();
//orWhereColumn省略

// 取出字段1>字段2的结果
$tests = DB::table('test')->whereColumn('age', '>','weight')->get();

$tests = DB::table('test')->whereColumn([
	['age', '=', 'weight'],
	['age', '>', 'weight'],
])->get();

// 嵌套的参数分组
// select * from test where name = 'ddddd' and (age > 20 or weight = '22')
$tests = DB::table('test')->where('name', '=', 'ddddd')->where(function ($query) {
	$query->where('age', '>', 20)->orWhere('weight', '=', '22');
})->get();
	
//select * from test where exists (select 1 from testjoin where test.id = testjoin.test_id)
$tests = DB::table('test')->whereExists(function ($query) {
	$query->select(DB::raw(1))
		 ->from('testjoin')
		 ->whereRaw('test.id = testjoin.test_id');
})->get();

echo DB::table('test')->where('age', 22)->exists();// 判断记录是否存在
// doesntExist()省略

(4)原生方法查询

// 原生方法 selectRaw()
// select age * 3 as age2 from test
$tests = DB::table('test')->selectRaw('age * ? as age2', [3])->get();

// 原生方法 whereRaw() / orWhereRaw()
// select *from test where age > IF( name = "ddddd", 20, 100 )
$tests = DB::table('test')->whereRaw('age > IF(name = "ddddd", ?, 100)', [20])->get();

// 原生方法 orderByRaw()
$tests = DB::table('test')->orderByRaw('id ASC')->get();

(5)聚合方法查询

$result = DB::table('test')->count();

$result = DB::table('test')->max('age');

$result = DB::table('test')->min('age');

$result = DB::table('test')->avg('age');

$result = DB::table('test')->sum('age');

(6)联合查询

// inner join
$test = DB::table('test')
	->select('test.*','testjoin.title')
	->join('testjoin','test.id','=','testjoin.test_id')
	->get();

// Left Join
$test = DB::table('test')
	->select('test.id as ids','test.name','test.age','testjoin.*')
	->leftJoin('testjoin', 'test.id', '=', 'testjoin.test_id')
	->get();
	

二,常用插入

// 单行插入
DB::table('test')->insert(
	[
		'name' => 'testtest', 
		'age' => 123, 
		'weight' => 123123,
		'dateTimeTest'=>'2019-10-09 13:40:11',
		'timeStampTest'=>'2019-10-09 13:40:11'
	]);

// 多行插入
DB::table('test')->insert([
	[
		'name' => 'testtest', 
		'age' => 123, 
		'weight' => 123123,
		'dateTimeTest'=>'2019-10-09 13:40:11',
		'timeStampTest'=>'2019-10-09 13:40:11'
	],
	[
		'name' => 'testtest', 
		'age' => 123, 
		'weight' => 123123,
		'dateTimeTest'=>'2019-10-09 13:40:11',
		'timeStampTest'=>'2019-10-09 13:40:11'
	]
]);

// 插入后取得自增ID
$id = DB::table('test')->insertGetId(
	[
		'name' => 'testtest1', 
		'age' => 123,
		 'weight' => 123123,
		 'dateTimeTest'=>'2019-10-09 13:40:11',
		 'timeStampTest'=>'2019-10-09 13:40:11'
	]
);
	

三,常用更新

$result = DB::table('test')->where('id', 8)->update(['name' => 'asdasdasd']);

DB::table('test')->increment('age');//自增1,此时数据表里全部被自增1

DB::table('test')->increment('age', 5);//自增5,上同

DB::table('test')->decrement('weight');//自减1,上同

DB::table('test')->decrement('weight', 5);//自减5,上同

// 给age增1时把名字也改掉,条件是id=2
DB::table('test')->where('id','=','2')->increment('age', 1, ['name' => 'sdsdsdsd']);

四,常用删除

DB::table('test')->where('id', '=', 9)->delete();// 删除

DB::table('test')->truncate();// 删除所有行,并重置自增 ID 为零

// 悲观锁。防止选中的数据列被篡改,直到事务被提交为止
DB::table('test')->where('age', '>', 100)->lockForUpdate()->get();

你可能感兴趣的:(laravel)