Home  >  Article  >  Backend Development  >  yii2.0 database query operation

yii2.0 database query operation

伊谢尔伦
伊谢尔伦Original
2016-12-02 10:40:481193browse

The following introduces some simple operations of Yii2.0 for database query

User::find()->all();    此方法返回所有数据;
User::findOne($id);   此方法返回 主键 id=1 的一条数据(举个例子); 
User::find()->where(['name' => '小伙儿'])->one();   此方法返回 ['name' => '小伙儿'] 的一条数据;
User::find()->where(['name' => '小伙儿'])->all();   此方法返回 ['name' => '小伙儿'] 的所有数据;
User::find()->orderBy('id DESC')->all();   此方法是排序查询;
User::findBySql('SELECT * FROM user')->all();  此方法是用 sql  语句查询 user 表里面的所有数据;
User::findBySql('SELECT * FROM user')->one();  此方法是用 sql  语句查询 user 表里面的一条数据;
User::find()->andWhere(['sex' => '男', 'age' => '24'])->count('id');   统计符合条件的总条数;
User::find()->one();    此方法返回一条数据;
User::find()->all();    此方法返回所有数据;
User::find()->count();    此方法返回记录的数量;
User::find()->average();    此方法返回指定列的平均值;
User::find()->min();    此方法返回指定列的最小值 ;
User::find()->max();    此方法返回指定列的最大值 ;
User::find()->scalar();    此方法返回值的第一行第一列的查询结果;
User::find()->column();    此方法返回查询结果中的第一列的值;
User::find()->exists();    此方法返回一个值指示是否包含查询结果的数据行;
User::find()->batch(10);  每次取 10 条数据 
User::find()->each(10);  每次取 10 条数据, 迭代查询

Yii provides us with a very rich set of queries


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Use php to detect proxyNext article:Use php to detect proxy