Back to Top

How to Apply Sub Queries on Collection in Magento 2

Updated 28 February 2024

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.

Start your headless eCommerce
now.
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home