2

When I want select where IN query, how to define multiple values in where clause?

Note on the ':k'=>1 and ':k'=>, how to use it for 2 values?

$query = Model::find()->where('id = :id and type = :k' ,[':id'=>$id, ':k'=>1,':k'=>27])->count();

3 Answers 3

2

Conditions can be also defined using array syntax:

$count = Model::find()
    ->where([
        'AND',
        ['=', 'id', $id],
        ['IN', 'type', [1, 27]],
    ])
    ->count();
Sign up to request clarification or add additional context in comments.

Comments

1

You could try using IN baded on an array assuming

$myArray = array(1,27);
$query = Model::find()->where(['IN', 'id', $myArray])
    ->andWhere('id = :id', [':id' => $id])->count();

Comments

1

You can do it in this way:

$types = [1, 27];
$query = Model::find()
    ->where(['id' => $id])
    ->andWhere(['type' => $types])
    ->count();

Yii2 will convert your $types array to IN condition. SQL query will be:

SELECT COUNT(*) FROM <table> WHERE id = <id> AND type IN (1, 27);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.