. 查询所有
Article::findAll(['status'=>1]);
2.查询一条
Article::findOne(1); //根据ID查询
Article::findOne(['status' => 1]); //根据条件查询
3.find()方法返回yii\db\ActiveQuery查询
Article::find()->where(['id'=>1])->one(); //ID等于1的一条数据
Article::find()->where(['status'=>1])->all(); //状态等于1的所有数据
Article::find()->where('status=:status',[':status'=>1])->all(); //状态等于1的所有数据
//查询状态等于1的数据并根据pubdate排序
Article::find()->where(['status'=>1])->orderBy('pubdate DESC')->all();
//查询状态等于1的数据并根据pubdate排序,从第10条开始,取4条
Article::find()->where(['status'=>1])->orderBy('pubdate ASC')->offset(10)->limit(4)->all();
MySql
$db = \Yii::$app->db;
可通过$db->tablePrefix获取表前缀如果有的话
1.查询一条
$db->createCommand('SELECT * FROM `article`')->queryOne();
2.绑定单个防SQL注入参数
$db->createCommand('SELECT * FROM `article` WHERE id=:id')->bindValue(":id",2)->queryOne();
3.绑定多个防SQL注入参数
$db->createCommand('SELECT * FROM `article` WHERE id=:id AND status=:status')->bindValues([':id'=>1,':status'=>1])->queryOne();
4.查询多条
$db->createCommand('SELECT * FROM `article`')->queryAll();
5.统计查询
$db->createCommand('SELECT COUNT("id") FROM `article`')->queryScalar();