首页 > 代码库 > 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 数据库 - 增删查改