In this case,you need to create custom mysql code
which will create mysql like this
Call your collection:
$collections = Mage::getResourceModel('module/namespace_collection');
Call the db resource singleton model
$resource = Mage::getSingleton('core/resource');
Carry connection write connection
$writeConnection = $resource->getConnection('core_write');
Make a custom collection by calling write connection and make a custom subquery:
$subSelect =$writeConnection->select()
->from(
array(' table2' => $collections->getTable('module/namespace')),
array( 'sku', 'MaxDate'=>new Zend_Db_Expr("max(date)"))
)
->group('table2.sku')
Join table
$collections->getSelect()->join(
array(' tm' => new Zend_Db_Expr(sprintf('(%s)',$subSelect->assemble()))),
'main_table.sku = tm.sku and main_table.date = tm.MaxDate,
array()
);
Select your selective columns
$collections->addFieldToSelect('sku')->addFieldToSelect('date');
Code is not tested and put basic of concept
1) I Assume you have created Model and Collection file associated with that tables.
2) In a Block PHP file constructor add one argument (Dependency Injection) like below and store it in a class member variable.
public function __construct(
Context $context,
\Namespace\Modulename\Model\ModelNameFactory $modelNameFactory,
array $data = array()
) {
$this->_modelFactory = $modelFactory;
parent::__construct($context, $data);
}
3) Prepare a public method in your block to access collection like below.
public function getCollection(){
return $this->_modelFactory->create()->getCollection();
}
4) Loop through each of the collection result.
Hope, this will help you.
Best Answer
If you have the order item id, then the sku for the order item should be the same as the sku for the invoice item. so you don't need to go to the invoice items table to get it.
but if you insist on getting it from the invoice item table do this: