《PHP實例:Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié)》要點:
本文介紹了PHP實例:Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié),希望對您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
User::find()->all(); //返回所有用戶數(shù)據(jù);
PHP學(xué)習(xí)
User::findOne($id); //返回 主鍵 id=1 的一條數(shù)據(jù);
PHP學(xué)習(xí)
User::find()->where(['name' => 'ttt'])->one(); //返回 ['name' => 'ttt'] 的一條數(shù)據(jù);
PHP學(xué)習(xí)
User::find()->where(['name' => 'ttt'])->all(); //返回 ['name' => 'ttt'] 的所有數(shù)據(jù);
PHP學(xué)習(xí)
User::findBySql('SELECT * FROM user')->all(); //用 sql 語句查詢 user 表里面的所有數(shù)據(jù);
PHP學(xué)習(xí)
User::findBySql('SELECT * FROM user')->one(); 此方法是用 sql 語句查詢 user 表里面的一條數(shù)據(jù);
PHP學(xué)習(xí)
User::find()->andWhere(['sex' => '女', 'age' => '18'])->count('id'); //統(tǒng)計符合條件的總條數(shù);
PHP學(xué)習(xí)
User::find()->one(); //返回一條數(shù)據(jù);
PHP學(xué)習(xí)
User::find()->all(); //返回所有數(shù)據(jù);
PHP學(xué)習(xí)
User::find()->count(); //返回記錄的數(shù)量;
PHP學(xué)習(xí)
User::find()->average(); //返回指定列的平均值;
PHP學(xué)習(xí)
User::find()->min(); //返回指定列的最小值 ;
PHP學(xué)習(xí)
User::find()->max(); //返回指定列的最大值 ;
PHP學(xué)習(xí)
User::find()->scalar(); //返回值的第一行第一列的查詢結(jié)果;
PHP學(xué)習(xí)
User::find()->column(); //返回查詢結(jié)果中的第一列的值;
PHP學(xué)習(xí)
User::find()->exists(); //返回一個值指示是否包含查詢結(jié)果的數(shù)據(jù)行;
PHP學(xué)習(xí)
查詢操作:
PHP學(xué)習(xí)
User::find()->where(['name' => 'username'])->one(); 此方法返回 ['name' => 'username'] 的一條數(shù)據(jù);
PHP學(xué)習(xí)
User::find()->where(['name' => 'username'])->all(); 此方法返回 ['name' => 'username'] 的所有數(shù)據(jù);
PHP學(xué)習(xí)
User::find()->andWhere(['sex' => '男', 'age' => '24'])->count('id'); 統(tǒng)計符合條件的總條數(shù);
PHP學(xué)習(xí)
新增操作:
PHP學(xué)習(xí)
$model = newUser();
PHP學(xué)習(xí)
$model->username = 'username';
PHP學(xué)習(xí)
$model->age = '20';
PHP學(xué)習(xí)
$model->insert();
PHP學(xué)習(xí)
修改操作:PHP學(xué)習(xí)
$User = User::findOne($id);
PHP學(xué)習(xí)
$User->name = 'zhangsan';
PHP學(xué)習(xí)
$User->save(); // 等同于 $User->update();
PHP學(xué)習(xí)
刪除操作:
PHP學(xué)習(xí)
User::deleteAll('name = username'); 刪除 name = username 的數(shù)據(jù);
PHP學(xué)習(xí)
User::findOne($id)->delete(); 刪除主鍵為 $id變量 值的數(shù)據(jù)庫;
PHP學(xué)習(xí)
User::deleteAll('age > :age AND sex = :sex', [':age' => '20', ':sex' => '1']); 刪除符合條件的數(shù)據(jù);
PHP學(xué)習(xí)
以上所述是小編給大家介紹的Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的.在此也非常感謝大家對維易PHP網(wǎng)站的支持!PHP學(xué)習(xí)
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/3950.html