L
L
Leopandro2017-07-23 21:27:46
MySQL
Leopandro, 2017-07-23 21:27:46

Is it possible in yii2?

How to insert values ​​from an array instead of $ids so that the query works? I tried everything already spent 6 hours on it.

$ids = [1,2,3,4,5,6];
$query->select('product.id, product.name, product_id, 
COUNT(CASE WHEN product_ingredient.ingredient_id IN ('.$ids.') 
THEN 1 END) AS ing_count');

if you bind as a parameter, then an array is inserted in quotes, like a string. If the subzarpros is done through Expression, then it still quotes the values ​​of the array.
implode is not a solution, because exits IN (1, `2`, `3`, `4`, `5`, `6) in quotes

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Soslan Aldatov, 2017-07-23
@sptm

$ids = [1,2,3,4,5,6];
$query->select('product.id, product.name, product_id, 
COUNT(CASE WHEN product_ingredient.ingredient_id IN ('.implode(",", $ids).') 
THEN 1 END) AS ing_count');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question