Category Archives: Yii

Yii count query records


Yii count query records Syntax And Example : Yii Count query result as below.


Yii join query records example with Syntax

$count = (new \yii\db\Query())
    ->from('users')
    ->where(['id' => '100'])
    ->count();

Which will generate the following query as below.

//Select count(*) from users where id=’100′;

where $id = 5;

Yii join query where condition


Yii join query where condition Syntax And Example : Yii join is used to get data collectively from one or more than one table.


Yii join query where condition Syntax

$customers = Users::find()
    ->select('users.*')
    ->leftJoin('order', '`order`.`user_id` = `user`.`id`')
    ->where(['order.status' => '1'])
    ->with('orders')
    ->all();

Which will result the row where id is equal to $id. The above query will produce the following result.

//Select id,name, email from users where id = “5”

where $id = 5;

Yii select distinct criteria Query


Yii select distinct criteria Query Syntax And Example : Yii Query builder is used to select records from table.


Yii select distinct criteria Query Syntax

$criteria = new CDbCriteria();
$criteria->distinct=true;
$criteria->condition = "id=".$id ;      
$criteria->select = 'id,name,email';
$modal=Users::model()->find($criteria);

Which will return the distinct row from table.

//SELECT DISTINCT id,name, email FROM users WHERE id = “5”

where $id = 5;

Yii Select query


Yii Select query Syntax And Example : Yii Query builder is used to select records from table.


Syntax : Yii Select query

$results = Yii::app()->db->createCommand()
    ->select('id,name, email')
    ->from('users')
    ->where('id=:id', array(':id'=>$id))
    ->queryRow();

Which will result the row where id is equal to $id. The above query will produce the following result.

//Select id,name, email from users where id = “5”

where $id = 5;