yii20数据库操作1

1、添加一条数据$post = new post;
$post->name = ‘yayue’;
$post->age = 24;
$post->save();
$pk = $post->getprimarykey(); //返回主键id
//也可以这么写
$array = [
‘name’ => ‘yayue’,
‘age’ => 24,
];
$post->setattributes($array);
$post->save();

2、查找数据

//根据主键获取一条数据
$data= $post->findone($pk);
$data= $post->findone([‘pk’=>1,’name’=>’yayue’]);
$data= $post->find()->where([‘id’=>$pk])->one();
//获取age=24多条数据
$data= $post->find()->where([‘age’=>24])->all();
$data= $post->findall([‘name’=>’yayue’,’age’=>24]);
//获取age=24并且name=yayue多条数据
$data= $post->find()->where([‘age’=>24])->andwhere([‘name’=>’yayue’])->all();
//获取age=24或者name=yayue多条数据
$data= $post->find()->where([‘age’=>24])->orwhere([‘name’=>’yayue’])->all();
//获取指定字段name、age age=24多条数据
$data= $post->find()->select([‘name’,’age’])->where([‘age’=>24])->all();
//统计age=24数据
$data= $post->find()->where([‘age’=>24])->cout();
//查询age最大的数据
$data= $post->find()->max(‘age’);
//查询age最小的数据
$data= $post->find()->min(‘age’);
//获取数据通过age排序
$data= $post->find()->orderby(‘age desc’)->all();
//获取age=24并且name=yayue多条数据
$data= $post->find()->where([‘age’=>24])->andwhere([‘name’=>’yayue’])->all();
//获取age=24或者name=yayue多条数据
$data= $post->find()->where([‘age’=>24])->orwhere([‘name’=>’yayue’])->all();
//获取指定字段name、age age=24多条数据
$data= $post->find()->select([‘name’,’age’])->where([‘age’=>24])->all();
//统计age=24数据
$data= $post->find()->where([‘age’=>24])->cout();
//查询age最大的数据
$data= $post->find()->max(‘age’);
//查询age最小的数据
$data= $post->find()->min(‘age’);
//获取数据通过age排序
$data= $post->find()->orderby(‘age desc’)->all();
//获取age=24并且name=yayue多条数据
$data= $post->find()->where([‘age’=>24])->andwhere([‘name’=>’yayue’])->all();
//获取age=24或者name=yayue多条数据
$data= $post->find()->where([‘age’=>24])->orwhere([‘name’=>’yayue’])->all();
//获取指定字段name、age age=24多条数据
$data= $post->find()->select([‘name’,’age’])->where([‘age’=>24])->all();
//统计age=24数据
$data= $post->find()->where([‘age’=>24])->count();
//查询age最大的数据
$data= $post->find()->max(‘age’);
//查询age最小的数据
$data= $post->find()->min(‘age’);
//获取数据通过age排序
$data= $post->find()->orderby(‘age desc’)->all();
//获取数据age>24
$data= $post->find()->where(‘age > :age’,[‘age’=>24])->all();
//获取数据age=24 or age=25
$data = $post->find()->where([‘or’,’age=24′, ‘age=25’])->all();
//其它where条件
// where id=1 or id=2
where(‘);
// where id=:id1 or id=:id2
where(‘, [‘:id1’=>1, ‘:id2’=>2]);
// where id=1 or id=2
where([‘or’, ‘, ‘]);
// where id=1 and (type=2 or type=3)
where([‘and’, ‘, [‘or’, ‘type=2’, ‘type=3’]]);
// where `id` in (1, 2)
where([‘in’, ‘id’, [1, 2]]);
// where `id` not in (1, 2)
where([‘not in’, ‘id’, [1,2]]);
// where `name` like ‘%qiang%’
where([‘like’, ‘name’, ‘%qiang%’]);
// where `name` like ‘%qiang’ and `name` like ‘%xue’
where([‘like’, ‘name’, [‘%qiang’, ‘%xue’]]);
// where `name` like ‘%qiang’ or `name` like ‘%xue’
where([‘or like’, ‘name’, [‘%qiang’, ‘%xue’]]);
// where `name` not like ‘%qiang%’
where([‘not like’, ‘name’, ‘%qiang%’]);
// where `name` not like ‘%qiang%’ or `name` not like ‘%xue%’
where([‘or not like’, ‘name’, [‘%qiang%’, ‘%xue%’]]);

以上就介绍了yii20数据库操作1,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。

Posted in 未分类