


1. Simple query:
]: 根据查询结果返回查询的第一条记录。 [[all()]]: 根据查询结果返回所有记录。 [[count()]]: 返回记录的数量。 [[sum()]]: 返回指定列的总数。 [[average()]]: 返回指定列的平均值。 [[min()]]: 返回指定列的最小值。 [[max()]]: 返回指定列的最大值。 [[scalar()]]: 返回查询结果的第一行中的第一列的值。 [[column()]]: 返回查询结果中的第一列的值。 [[exists()]]: 返回一个值,该值指示查询结果是否有数据。 [[where()]]: 添加查询条件 [[with()]]: 该查询应执行的关系列表。 [[indexBy()]]: 根据索引的列的名称查询结果。 [[asArray()]]: 以数组的形式返回每条记录。 [code=php]Customer::find()->one(); 此方法返回一条数据; Customer::find()->all(); 此方法返回所有数据; Customer::find()->count(); 此方法返回记录的数量; Customer::find()->average(); 此方法返回指定列的平均值; Customer::find()->min(); 此方法返回指定列的最小值 ; Customer::find()->max(); 此方法返回指定列的最大值 ; Customer::find()->scalar(); 此方法返回值的第一行第一列的查询结果; Customer::find()->column(); 此方法返回查询结果中的第一列的值; Customer::find()->exists(); 此方法返回一个值指示是否包含查询结果的数据行;
Customer::find()->asArray()->one(); 以数组形式返回一条数据; Customer::find()->asArray()->all(); 以数组形式返回所有数据;
Customer::find()->where($condition)->asArray()->one(); 根据条件以数组形式返回一条数据; Customer::find()->where($condition)->asArray()->all(); 根据条件以数组形式返回所有数据;
Customer::find()->where($condition)->asArray()->orderBy('id DESC')->all(); 根据条件以数组形式返回所有数据,并根据ID倒序;
2. Related query:
[[ActiveRecord::hasOne()]]:返回对应关系的单条记录 [[ActiveRecord::hasMany()]]:返回对应关系的多条记录[/code]
Application example:
); } public function getCountry() { //客户和国家是一对一的关系所以用hasOne return $this->hasOne(CountrysModel::className(), ['id'=>'Country_id']); } .... } // 查询客户与他们的订单和国家 CustomerModel::find()->with('orders', 'country')->all(); // 查询客户与他们的订单和订单的发货地址 CustomerModel::find()->with('orders.address')->all(); // 查询客户与他们的国家和状态为1的订单 CustomerModel::find()->with([ 'orders' => function ($query) { $query->andWhere('status = 1'); }, 'country', ])->all(); :with中的orders对应getOrders <b>常见问题:</b> 1.在查询时加了->select();如下,要加上order_id,即关联的字段(比如:order_id)比如要在select中,否则会报错:undefined index order_id // 查询客户与他们的订单和国家 CustomerModel::find()->select('order_id')->with('orders', 'country')->all();
)->one(); // 查询年龄为30,状态值为1的客户 $customer = Customer::findOne(['age' => 30, 'status' => 1]); $customer = Customer::find()->where(['age' => 30, 'status' => 1])->one(); // 查询key值为10的所有客户 $customers = Customer::findAll(10); $customers = Customer::find()->where(['id' => 10])->all(); // 查询key值为10,11,12的客户 $customers = Customer::findAll([10, 11, 12]); $customers = Customer::find()->where(['id' => [10, 11, 12]])->all(); // 查询年龄为30,状态值为1的所有客户 $customers = Customer::findAll(['age' => 30, 'status' => 1]); $customers = Customer::find()->where(['age' => 30, 'status' => 1])->all(); <b>where()条件:</b> $customers = Customer::find()->where($cond)->all(); $cond写法举例: // SQL: (type = 1) AND (status = 2). $cond = ['type' => 1, 'status' => 2] // SQL:(id IN (1, 2, 3)) AND (status = 2) $cond = ['id' => [1, 2, 3], 'status' => 2] //SQL:status IS NULL $cond = ['status' => null] <b>[[]]</b>:将不同的条件组合在一起,用法举例: //SQL:`id=1 AND id=2` $cond = ['and', 'id=1', 'id=2'] //SQL:`type=1 AND (id=1 OR id=2)` $cond = ['and', 'type=1', ['or', 'id=1', 'id=2']]
[[or]]:
], ['id' => [1, 2, 3]] <b>[[]]:</b> //SQL:`NOT (attribute IS NULL)` $cond = ['not', ['attribute' => null]]
[[between]]: not between Same usage
<b>[[]]: </b>not in 用法类似 //SQL:`id IN (1, 2, 3)` $cond = ['in', 'id', [1, 2, 3]] //IN条件也适用于多字段 $cond = ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]] //也适用于内嵌sql语句 $cond = ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
[[like]]:
//SQL:`name LIKE '%test%' AND name LIKE '%sample%'` $cond = ['like', 'name', ['test', 'sample']] //SQL:`name LIKE '%tester'` $cond = ['like', 'name', '%tester', false] <b>[[]]: </b>not exists用法类似 //SQL:EXISTS (SELECT "id" FROM "users" WHERE "active"=1) $cond = ['exists', (new Query())->select('id')->from('users')->where(['active' => 1])]
Also, you can specify any operator as follows
//SQL:`id >= 10` $cond = ['>=', 'id', 10] //SQL:`id != 10` $cond = ['!=', 'id', 10]
Commonly used Query:
// WHERE admin_id >= 10 LIMIT 0,10 User::find()->select('*')->where(['>=', 'admin_id', 10])->offset(0)->limit(10)->all() // SELECT `id`, (SELECT COUNT(*) FROM `user`) AS `count` FROM `post` $subQuery = (new Query())->select('COUNT(*)')->from('user'); $query = (new Query())->select(['id', 'count' => $subQuery])->from('post'); // SELECT DISTINCT `user_id` ... User::find()->select('user_id')->distinct();
Update:
//update(); //runValidation boolen 是否通过validate()校验字段 默认为true //attributeNames array 需要更新的字段 $model->update($runValidation , $attributeNames); //updateAll(); //update customer set status = 1 where status = 2 Customer::updateAll(['status' => 1], 'status = 2'); //update customer set status = 1 where status = 2 and uid = 1; Customer::updateAll(['status' => 1], ['status'=> '2','uid'=>'1']);
Delete:
$model = Customer::findOne($id); $model->delete(); $model->deleteAll(['id'=>1]);
Batch insert:
Yii::$app->db->createCommand()->batchInsert(UserModel::tableName(), ['user_id','username'], [ ['1','test1'], ['2','test2'], ['3','test3'], ])->execute();
View execution sql
//UserModel $query = UserModel::find()->where(['status'=>1]); echo $query->createCommand()->getRawSql();
The above is the detailed explanation of the addition, deletion, modification and query of Yii2.0 database operations. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Go语言是一种高效、简洁且易于学习的编程语言,因其在并发编程和网络编程方面的优势而备受开发者青睐。在实际开发中,数据库操作是不可或缺的一部分,本文将介绍如何使用Go语言实现数据库的增删改查操作。在Go语言中,我们通常使用第三方库来操作数据库,比如常用的sql包、gorm等。这里以sql包为例介绍如何实现数据库的增删改查操作。假设我们使用的是MySQL数据库。

JavaList接口是Java中常用的数据结构之一,可以方便地实现数据的增删改查操作。本文将通过一个示例来演示如何使用JavaList接口来实现数据的增删改查操作。首先,我们需要在代码中引入List接口的实现类,常见的有ArrayList和LinkedList。这两个类都实现了List接口,具有类似的功能但底层实现方式不同。ArrayList是基于数组实

MySql是一种关系型数据库管理系统,在Web应用程序中非常常用。在整个Web应用开发过程中,CRUD(增删改查)操作是必不可少的。这篇文章将介绍如何在MySql中快速完成这些操作。增加(Create)在MySql中,我们使用INSERTINTO语句来插入新的行。例如,我们有一个名为“users”的表格,包含“id”,“name”和“email”三列。现在

如何在Java中使用集合框架函数进行集合的增删改查操作在Java中,集合框架(CollectionFramework)提供了一系列类和接口来方便我们进行集合操作。这些类和接口包含了丰富的函数,可以让我们更加方便地对集合进行增加、删除、修改和查找等操作。下面我们将详细介绍如何使用集合框架函数进行这些操作,并提供具体的代码示例。集合的增加操作在Java中,可以

Vue技术开发中如何处理表单数据的增删改查操作在Vue技术开发中,表单数据的增删改查操作是非常常见的需求。本文将介绍如何使用Vue技术处理这些操作,并提供具体的代码示例。首先,我们需要创建一个Vue实例,并在data属性中定义一个空数组来存储表单数据。例如:newVue({data(){return{formData:[

Java中JSON数组的增删改查操作技巧分享引言:JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,广泛应用于各种互联网应用中。在Java中,我们可以通过使用一些第三方库,比如GSON、Jackson等,来对JSON进行操作。本文将会分享在Java中对JSON数组进行增删改查操作的一些技巧,并提供相应的代码示例。一、

Python中的XML数据的增删改查操作XML(可扩展标记语言)是一种用于存储和传输数据的文本格式。在Python中,我们可以使用多种库来处理XML数据,其中最常用的是xml.etree.ElementTree库。本文将介绍如何使用Python对XML数据进行增删改查的操作,并通过代码示例加以说明。一、引入模块首先,我们需要引入xml.etree.Eleme

如何使用thinkorm快速实现数据库的增删改查功能引言:数据库是现代软件应用中不可或缺的一部分,而对数据库进行增加、删除、修改和查询等常见操作是开发过程中必不可少的。ThinkORM是一种功能强大且易于使用的Python数据库框架,它可以帮助我们快速实现这些操作。本文将介绍如何使用ThinkORM来实现数据库的增删改查功能,并提供相应的代码示例。关键词:T


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function