How to Join 2 Tables in Magento 2
Magento 2 stores data in database tables of customers, orders, products, etc.
The admin may often require to join data of two tables and it can be done using the below solution.
Join 2 tables in Magento 2 while working on product collection data or customers, orders, etc. collection data.
For instance, you need to get the data of all the orders completed through the PayPal payment method, or you need to get the data of customers from a specific country.
All such operations can be done by joining data between 2 tables in Magento 2 as shown below:
Method to Join 2 Tables in Magento 2:
1 2 3 4 5 6 |
$select = $collection->getSelect(); $select->joinLeft( ["boleta_number" => $collection->getTable("boleta_number")], 'main_table.entity_id = boleta_number.order_id' ); // $collection is an order collection |
Any doubts about a method to join two tables in Magento 2 can be mentioned in the Comments section below. I’d be happy to help.
Also, do share the post with Magento Community via social media.
Thank you.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
How to Add New Field in Cart Price Rules Form in Magento 2 Backend
How to Set Image Gallery Fotorama Thumbnail as Vertical View in Magento 2
Next