Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
When I want select where IN query, how to define multiple values in where clause?
IN
Note on the ':k'=>1 and ':k'=>, how to use it for 2 values?
':k'=>1 and ':k'=>
$query = Model::find()->where('id = :id and type = :k' ,[':id'=>$id, ':k'=>1,':k'=>27])->count();
Conditions can be also defined using array syntax:
$count = Model::find() ->where([ 'AND', ['=', 'id', $id], ['IN', 'type', [1, 27]], ]) ->count();
Add a comment
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();
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:
$types
SELECT COUNT(*) FROM <table> WHERE id = <id> AND type IN (1, 27);
Required, but never shown
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.
Explore related questions
See similar questions with these tags.