Magento how to join a collection with SQL

Joining tables in Magento is relatively easy.

// Create som ecollection.
$collection = Mage::getModel('catalog/product')->getCollection();

// Join this collection with sizes.
                $collection->getSelect()->joinLeft(
                    array( 'sizes' => 'catalog_product_entity_int' ),
                    'e.entity_id = sizes.entity_id AND sizes.attribute_id = 132', // 132 is id of sizes attribute
                    array( 'size' => 'sizes.value' ));

This will produce the following SQL: SELECT `e`.*, `sizes`.`value` AS `size` FROM `catalog_product_entity` AS `e` LEFT JOIN `catalog_product_entity_int` AS `sizes` ON e.entity_id = sizes.entity_id AND sizes.attribute_id = 132 In case you want to do another type of join, there is also joinRight or joinInner


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.