$user = Db::query('select * from `user`');
$user=Db::table('user')->where('UserID',1001)->find();//查询结果为空 返回 null
$user=Db::table('user')->where('UserID',1001)->findOrEmpty();//查询结果为空 返回 空数组
$user=Db::table('user')->where('UserID',1001)->select();
$user=Db::table('user')->where('UserID', 1001)->select()->toArray();
$user=Db::table('user')->where('UserID',1002)->value('UserName');//只能返回一条数据
$user=Db::table('user')->where('Password',123456)->column('UserName');//可以返回多条数据
$data=['UserID'=>'1005','UserName'=>'Season','Password'=>'123456'];
Db::name('user')->insert($data);
DB::table('user')->where('UserID',1005)->update(['Password'=>123]);
Db::table('user')->delete([1002,1003]);
Db::table('user')->where('Password',123)->delete();
$user=Db::table('user')->where("UserID","=",1001)->select();
$user=Db::table('user')->where('UserName','like','s%')->select();
$user=Db::table('user')->where('UserID','between','1001,1003')->select();
$user=Db::table('user')->where('UserID','in',[1001,1003])->select();
$user=Db::table('user')->where('Password','not null')->select();
$user=Db::table('user')->where('Password',null)->select();
$user=Db::table('user')->where('Password',123456)->order('UserName')->select();
$user=Db::table('user')->field('UserID,UserName')->select();
$user=Db::table('user')->alias('a')->where('a.UserID',1001)->select();
$user=Db::table('user')->limit(2)->select();
$user=Db::table('user')->page(2,5)->select();
$user=Db::table('user')->order('UserID')->select();
$user=Db::table('user')->order('UserID','desc')->select();
$user=Db::table('user')->alias('u')->field('u.Status,SUM(u.Price)')->group('u.Status')->select();
$user=Db::table('user')->alias('u')->field('u.Status,SUM(u.Price)')->group('u.Status')->having('u.Status>0')->select();
INNER JOIN: 等同于 JOIN(默认的JOIN类型),如果表中有至少一个匹配,则返回行
LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
FULL JOIN: 只要其中一个表中存在匹配,就返回行
Db::Transaction(function (){
Db::name('user')->where('UserID',1001)->save(['Price'=>Db::raw('Price-3')]);
Db::name('user')->where('UserID',1002)->save(['Price'=>Db::raw('Price+3')]);
});
Db::startTrans();
try {
Db::name('user')->where('UserID',1001)->save(['Price'=>Db::raw('Price-3')]);
Db::name('user')->where('UserID',1002)->save(['Price'=>Db::raw('Price+3')]);
Db::commit();//提交事务
}catch (\Exception $e){
echo '执行SQL失败,开始回滚数据';
Db::rollback();
}
$user=Db::name('user')->withAttr('UserName',function ($value,$data){
var_dump($value);
var_dump($data);
})->select();;
手机扫一扫
移动阅读更方便
你可能感兴趣的文章