laravel 操作mysql数据库的三种方式

一、使用DB门面操作

需要引入 DB类 Illuminate\Support\Facades\DB

查询数据库记录

DB::select(SQL语句);

DB::select('select * from `stu` from ')	//以数组形式返回结果

增加数据库记录

DB::insert(SQL语句);

DB::insert("insert into `stu` (`name`,`sex`,`class_id`,`age`,`tall`) values ('刘素五','男',3,15,166);");

更新数据库记录

DB::update(SQL语句);

DB::update("update `stu` set `name` = '张三峰' where id = 10");

删除数据库记录

DB::delete(SQL语句);

	DB::delete("delete from `stu` where `id` = 21");

二、使用查询构造器

需要引入 DB类 Illuminate\Support\Facades\DB

查询数据库记录

DB::table('stu')->get(); //查询所有数据
DB::table('stu')->where('id','<',5)->where('id','>',3)->get();  //查询id大于3且小于5的记录
DB::table('stu')->all(); //查询所有数据,不能使用where条件
DB::table('stu')->select('name','sex')->get(); //查询name,sex两个字段
DB::table('stu')->orderBy('age','desc')->get();  //将查询结果按年龄升序显示
DB::table('stu')->orderBy('age','asc')->get()->first(); //查询年龄最大的一条记录
DB::table('stu')->limit(5)->get();  //取5条记录
DB::table('stu')->limit(5)->offset(2)->get();  //从第二条记录开始取5条记录
//每次从数据库中查询5条记录,直到查询完所有的记录
DB::table('stu')->chunk(5,function($students){
	var_dump($students);
	//return false;  //只查询一次
})-get(); 
DB::table('stu')->find(2); //查询id为2的记录

//聚合函数
DB::table('stu')->count(); 	   //返回所有记录数
DB::table('stu')->max('age');  //返回年龄最大的值
DB::table('stu')->min('age');  //返回年龄最小的值
DB::table('stu')->avg('age');  //返回年龄的平均值
DB::table('stu')->sum('age');  //返回年龄的总和

增加数据库记录

//增加多条记录
DB::table('stu')->insert([
	[
		'name' => '张三疯',
		'sex' => '男',
		'age' => 18
	],
	[
		'name' => '赵四颠',
		'sex' => '男',
		'age' => 18
	]
]); 

//增加一条记录
DB::table('stu')->insert([
	'name' => '夏月月',
	'sex' => '女',
	'age' => 18
]); 

DB::table('stu')->insertGetId([
	'name' => '孙磊磊',
	'sex' => '男',
	'age' =>18
]);

更新数据库记录

//修改id为5的记录
DB::table('stu')->where('id',5)->update([
	'name' => '慕容晓月',
	'sex' => '女',
	'age' => 23
]);

删除数据库记录

//删除id大于20的记录
DB::table('stu')->where('id','>',20)->delete();
//删除姓名为空的记录
DB::table('stu')->where('name',null)->delete();

三、使用模型

增删改查几乎和构造查询构造器差不多

站点目录下使用composer创建模型

#php artisan make:model Home/Student
//模型类中的代码

//定义关联的数据表
protected $table = 'stu';
//定义主键
protected $primaryKey = 'id';
//设置是否允许操作时间
public $timestamps = false;
//设置允许批量赋值的字段
protected $fillalble = ['name','sex','age','class_id','tall'];

查询数据库记录

Student::get(); //查询数据库的所有记录 返回的是对象
Student::get()->toarray();  //将查询结果转换为数组
Student::where('id','<',10)->get(); //查询id小于10的记录
Student::get()->first(); //取第一条记录
Student::orderBy('age','desc')->limit(5)->get();  //取年龄最大的5条记录
Student::limit(5)->offset(10)-get(); //从第10条记录开始截取5条记录

//聚合函数
Student::count(); 	   //返回所有记录数
Student::max('age');  //返回年龄最大的值
Student::min('age');  //返回年龄最小的值
Student::avg('age');  //返回年龄的平均值
Student::sum('age');  //返回年龄的总和
### 增加数据库记录
```php
//增加多条记录
Student::insert([
	[
		'name' => '张三疯',
		'sex' => '男',
		'age' => 18
	],
	[
		'name' => '赵四颠',
		'sex' => '男',
		'age' => 18
	]
]); 

//增加一条记录
Student::insert([
	'name' => '夏月月',
	'sex' => '女',
	'age' => 18
]); 

Student::insertGetId([
	'name' => '孙磊磊',
	'sex' => '男',
	'age' =>18
]);

更新数据库记录

//修改id为5的记录
Student::where('id',5)->update([
	'name' => '慕容晓月',
	'sex' => '女',
	'age' => 23
]);

删除数据库记录

//删除id大于20的记录
Student::where('id','>',20)->delete();
//删除姓名为空的记录
Student::where('name',null)->delete();
//删除主键id为5,10的记录
Student::destroy(5,10);
原创文章 7 获赞 3 访问量 399

猜你喜欢

转载自blog.csdn.net/weixin_42951763/article/details/105028477