{"id":1653,"date":"2021-03-20T13:58:45","date_gmt":"2021-03-20T13:58:45","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/join-2-tables-in-magento-2\/"},"modified":"2025-07-17T09:34:50","modified_gmt":"2025-07-17T04:04:50","slug":"join-2-tables-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/join-2-tables-in-magento-2\/","title":{"rendered":"How to Join 2 Tables in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0stores data in database tables of customers, orders, products, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The admin may often require to join data of two tables and it can be done using the below solution.<br><em><strong>Join 2 tables in Magento 2<\/strong><\/em>&nbsp;while working on product collection data or customers, orders, etc. collection data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All such operations can be done by joining data between 2 tables in Magento 2 as shown below:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Join 2 Tables in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$select = $collection->getSelect();\n$select->joinLeft(\n    [\"boleta_number\" => $collection->getTable(\"boleta_number\")],\n    'main_table.entity_id = boleta_number.order_id'\n);\n\/\/ $collection is an order collection<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do share the post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0stores data in database tables of customers, orders, products, etc. The admin may often require to join data of two tables and it can&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1653","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"predecessor-version":[{"id":18167,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1653\/revisions\/18167"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}