How two Join Two Custom Tables in Magento?

Magento join two tables:  To join two custom table in the database you need to add specific query with table to get the desired result. Here is the code that will impart solution for joining two tables in Magento.


Magento Join Two Custom Tables Syntax:

$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().
'table_name_for_join', 'main_table.your_table_field ='.Mage::getConfig()
->getTablePrefix().'table_name.table_field', 
array('field_name_you_want_to_fetch_from_db'));

Magento Join Two Custom Tables Example:

$collection = Mage::getModel('sales/order')->getCollection();
$collection->getSelect()->join( array('order_item'=> 'sales_flat_order_item'),
'order_item.order_id = main_table.entity_id',
array('order_item.sku'));

You may also like...

Leave a Reply

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