Heim > Backend-Entwicklung > PHP-Tutorial > Yii20数据库操作1

Yii20数据库操作1

WBOY
Freigeben: 2016-07-29 09:02:19
Original
900 Leute haben es durchsucht
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();
Nach dem Login kopieren

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('id=1 or id=2'); 
// WHERE id=:id1 or id=:id2 
where('id=:id1 or id=:id2', [':id1'=>1, ':id2'=>2]); 
// WHERE id=1 OR id=2   
where(['or', 'id=1', 'id=2']); 
// WHERE id=1 AND (type=2 OR type=3) 
where(['and', 'id=1', ['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%']]);
Nach dem Login kopieren

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage