How to use group by in magento collection with join query
-
12-12-2019 - |
Question
In module's admin grid I am using this code to get collection and group them by customer id
$collection = Mage::getModel('referafriend/statistics')->getCollection();
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
but here i have to use renderer and filter functions for customer info like name and email against each entity_id
.
i want to join customer model with my module's table. for this i have written this code
$collection = Mage::getModel('customer/customer')->getCollection()
->addNameToSelect();
$collection->getSelect()->join(array('refer' => 'table_name'),'refer.entity_id = e.entity_id'
);
$collection->getSelect()->group('entity_id');
$collection->addAttributeToSelect('*');
but it gives me this error
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'entity_id' in group statement is ambiguous
any help will be highly appreciated.
Solution
You need to add table name in group by condition
.As you did not added on conditions table name
at table group('entity_id') so query did not find columns name
getSelect()->group('e.entity_id');
Logic is:
$collection->getSelect()->group('TABLE_NAME.FIELD_NAME')
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange