首页 > 代码库 > laravel 数据库 - 增删查改
laravel 数据库 - 增删查改
//查询
public function select(){
/**
数据表
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(255) DEFAULT ‘1‘,
`create_at` int(11) DEFAULT ‘0‘,
`score` int(3) DEFAULT ‘0‘ COMMENT ‘分数‘,
`class_id` int(11) DEFAULT NULL COMMENT ‘班级id‘,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
CREATE TABLE `student_class` (
`class_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`is_show` int(11) DEFAULT NULL,
PRIMARY KEY (`class_id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
*/
$students = DB::table(‘student‘)->select(‘name as user_name‘)->get();
$user = DB::table(‘student‘)->where(‘name‘,‘Try‘)->first();
$name = DB::table(‘student‘)->where([‘name‘=>‘Try‘])->pluck(‘name‘);
$lists = DB::table(‘student‘)->lists(‘name‘);
$users = DB::table(‘Student‘)->where(‘age‘,‘>‘,22)->orWhere(‘sex‘,‘=‘,0)->get();
$users = DB::table(‘Student‘)->whereBetween(‘score‘,array(84,100))->get();
$users = DB::table(‘Student‘)->whereIn(‘score‘,array(85,95))->get();
$users = DB::table(‘Student‘)->whereNull(‘score‘)->get();
$users = DB::table(‘Student‘)->orderBy(‘score‘,‘desc‘)->having(‘score‘, ‘>‘, 84)->get();
$users = DB::table(‘Student‘)->join(‘Student_class‘,‘Student_class.class_id‘,‘=‘,‘Student.class_id‘)->get();
$users = DB::table(‘Student‘)->count();
$score = DB::table(‘Student‘)->max(‘score‘);
$score = DB::table(‘Student‘)->where(‘id‘,1)->sum(‘score‘,‘+‘,‘age‘);
//插入操作-批量插入
/*$id = DB::table(‘Student‘)->insert(
array(
array(‘name‘=>‘xiaohua‘,‘age‘=>8,‘sex‘=>0,‘score‘=>33),
array(‘name‘=>‘xiaocao‘,‘age‘=>9 ,‘sex‘=>0,‘score‘=>82)
)
);*/
$id = DB::table(‘Student‘)->where(‘id‘,5)->update(array(‘sex‘=>1));
$id = DB::table(‘Student‘)->where(‘name‘,‘xiaocao‘)->delete();
$first = DB::table(‘Student‘)->whereNull(‘class_id‘);
$users = DB::table(‘Student‘)->whereNull(‘score‘)->union($first)->get();
$lock = DB::table(‘Student‘)->where(‘score‘, ‘>‘,
85)->sharedLock()->get();
$lock_update = DB::table(‘Student‘)->where(‘score‘, ‘>‘, 95)->lockForUpdate()->get();
dd($lock_update);
}
laravel 数据库 - 增删查改
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。