$collection = $Custommodel->getCollection()->addFieldToFilter("customer_id",$customerid);

$collection->getSelect()->group('product_id')->order("viewed_date DESC");

while implement this way i getting wrong ordered records

what mistake there??

here this query runing magento SELECT main_table.* FROM wac_viewed_products AS main_table WHERE (customer_id = '4') GROUP BY product_id ORDER BY viewed_date DESC

  • 1
    Can you do ->__toString() to see what query is running and then try in MySQL console directly to see if what you are getting is what is expected?
    – Arif Ahmad
    Mar 17, 2020 at 13:05
  • getting error for that query Mar 17, 2020 at 13:09
  • what error you are getting? the script seems to generate correct query
    – Arif Ahmad
    Mar 18, 2020 at 6:32
  • i fixed this using MAX(column_name) and thanks to all who cmds this question Mar 18, 2020 at 6:50

1 Answer 1


Try with below code,

$collection = $this->collectionFactory->create()
           ->addFieldToFilter('id', $id)
           ->addFieldToFilter('status', 'Enable');

$collection->getSelect()->group('posts_id')->order('id DESC');
  • checked with this... updated question plz check Mar 17, 2020 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.