How to apply Sub Queries on Collection in Magento 2:
In this blog we will see how we can apply sub queries on collection in magento 2.
A sub query is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.
$collection->getSelect()
->columns([
'first_selected_column_name' => new \Zend_Db_Expr(
'select column_name From table_name where condition'
),
'second_selected_column_name' => new \Zend_Db_Expr(
'select column_name From table_name where condition'
)
]);
In this way we can apply nested or sub queries on collection. Hope it will be helpful for you. 🙂
For any query comment below.
Be the first to comment.