I'm trying to make query builder to output the following MySQL query:
SELECT p1.id, p1.product_id, p1.updated_at
FROM tbl_scrape_data p1
INNER JOIN
(
SELECT max(updated_at) MaxDate, product_id
FROM tbl_scrape_data
WHERE product_id IN (1,2,3)
GROUP BY product_id
) p2
ON p1.product_id = p2.product_id
AND p1.updated_at = p2.MaxDate
WHERE p1.product_id IN (1,2,3)
order by p1.updated_at desc
Here's what I tried:
$scrapeData = (new Query() )
->select(['p1.product_id', 'p1.id', 'p1.updated_at'])
->from('tbl_scrape_data p1')
->innerJoin([
'p2' => (new Query)
->select(['MAX(updated_at) MaxDate', 'product_id' ])
->from('tbl_scrape_data')
->where([ 'product_id' => [1, 2, 3, 15, 4] ])
->groupBy('product_id'),
//->all(),
['p1.product_id' => 'p2.product_id', 'p1.updated_at' => 'p2.MaxDate']
])
->where([ 'p1.product_id' => [1, 2, 3, 15, 4] ])
->orderBy('p1.updated_at DESC')
->all();
Yii2 throws an error trying to perform this query. Can someone tell me if this is a Yii2 bug or if I'm missing something? Or maybe the way I formatted the query builder is wrong?
I'm using Yii 2.0.1 and the error is
strpos() expects parameter 1 to be string, array given
\vendor\yiisoft\yii2\db\QueryBuilder.php at line 715