I have this working SQL query :
select prd_brand.* from prd_brand
inner join
(
select distinct value from catalog_product_entity_int
where
rowid in (select rowid from catalog_product_entity_int where attribute_id = 97 and value = 1)
)t
on prd_brand.brand_id = t.value
How can I nest this select query in the join in Magento 2 ?
I did it like this :
$subquery = new \Zend_Db_Expr('SELECT DISTINCT value from catalog_product_entity_int where rowid in (select rowid from catalog_product_entity_int where attribute_id = 97 and value = 1)');
$brandCollection->join(array(
'cpei' => 'catalog_product_entity_int'),
'cpei.value = main_table.brand_id',
array('subquery' => $subquery));
But it gives this query :
SELECT `main_table`.*,
SELECT DISTINCT value from catalog_product_entity_int
where rowid in
(select rowid from catalog_product_entity_int where attribute_id = 97 and value = 1) AS `subquery`
FROM `prd_brand` AS `main_table`
INNER JOIN `catalog_product_entity_int` AS `cpei` ON cpei.value = main_table.brand_id