Yii2简单数据库查询

Article::find();//创建一个查询实例

//select(): 设置查询的列名称(string|array)
Article::find()->select('id, name');
Article::find()->select(['id', 'name']); //同上
//sql: SELECT `id`, `name` FROM `article`

//where(): 设置查询的条件(string|array)
//andWhere(): 添加一个额外的查询条件
//orWhere(): 添加一个额外的查询条件
Article::find()->where(['name'=>'小伙儿']);
//sql: SELECT * FROM `article` WHERE `name`='小伙儿'

//orderBy(): 设置查询的排列顺序(string|array)
//addOrderBy(): 附加一个条件
Article::find()->orderBy('id ASC, name DESC');
Article::find()->orderBy(['id'=>SORT_ASC, 'name'=>SORT_DESC]); //同上
//sql: SELECT * FROM `article` ORDER BY `id`, `name` DESC

//limit(): 限定查询的数量(integet)
Article::find()->limit(5);
//sql: SELECT * FROM `article` LIMIT 5

//offset(): 限定查询的数量(integet), 与limit()结合使用
Article::find()->offset(3)->limit(5);
//sql: SELECT * FROM `article` LIMIT 5 OFFSET 3

Article::find()->one(); //返回一条数据
Article::find()->all(); //返回所有数据
Article::find()->count(); //返回记录的数量
Article::find()->sum('hits'); //返回指定列的总和
Article::find()->max('hits'); //返回指定列的最大值
Article::find()->min('hits'); //返回指定列的最小值
Article::find()->average('hits'); //返回指定列的平均值
Article::find()->scalar(); //返回查询结果的第一行的第一列的值
Article::find()->column(); //返回查询结果的第一列的值
Article::find()->exists; //返回一个值指示是否包含查询结果的数据行
Article::find()->batch(10); //每次取 10 条数据
Article::find()->each(10); //每次取 10 条数据, 迭代查询

Article::findOne(10); //返回主键为10的一条数据, 相当于 Article::find()->where(['id'=>10])->one();
Article::findAll(10); //返回主键为10的所有数据, 相当于 Article::find()->where(['id'=>10])->all();
Article::findAll([10,11,12]); //相当于 Article::find()->where(['id'=>[10, 11, 12]])->all();
Article::findAll(['age'=>30, 'status'=>1]); //相当于 Article::find()->where(['age'=>30, 'status'=>1])->all();
Article::findOne(['age'=>30, 'status'=>1]); //相当于 Article::find()->where(['age'=>30, 'status'=>1])->one();
Article::findBySql('SELECT * FROM user'); //用sql语句查询

where() 的更多用法参考yii2模糊查询where()的用法

待验证:

User::find()->addSelect();
User::find()->addOrderBy();
User::find()->groupBy();
User::find()->addGroupBy();
User::find()->attachBehavior();
User::find()->attachBehaviors();
User::find()->batch();
User::find()->behaviors();
User::find()->canGetProperty();
User::find()->canSetProperty();
User::find()->className();
User::find()->create();
User::find()->createCommand();
User::find()->detachBehavior();
User::find()->detachBehaviors();
User::find()->distinct();
User::find()->each();
User::find()->ensureBehaviors();
User::find()->exists();
User::find()->filterWhere();
User::find()->andFilterWhere();
User::find()->from();
User::find()->getBehavior();
User::find()->getBehaviors();
User::find()->hasEventHandlers();
User::find()->hasMethod();
User::find()->hasProperty();
User::find()->indexBy();
User::find()->init();
User::find()->off();
User::find()->offset();
User::find()->on();
User::find()->orFilterWhere();
User::find()->params();
User::find()->addParams();
User::find()->populate();
User::find()->prepare();
User::find()->trigger();
User::find()->union();
User::find()->having();
User::find()->andHaving();
User::find()->orHaving();
User::find()->join();    //附加一个连接部分查询
User::find()->leftJoin();    //附加部分左外连接查询
User::find()->rightJoin();    //附加部分右外连接查询
User::find()->innerJoin();    //附加部分内连接查询