Skip to main content
replaced http://magento.stackexchange.com/ with https://magento.stackexchange.com/
Source Link

I have followed Magento 2 use group by in collectionMagento 2 use group by in collection

I need to run below query

SELECT * FROM my_table WHERE status= 'Enable' GROUP BY id ORDER BY id DESC 

In collection factory i have below code

$collection = $this->collectionFactory->create()
           ->addFieldToFilter('id', $id)
           ->addFieldToFilter('status', 'Enable')
           ->setGroupBy('posts_id') // I tried to use group() as well
           ->setOrder('id','desc');
       

How to set Group By on above collection?

I have followed Magento 2 use group by in collection

I need to run below query

SELECT * FROM my_table WHERE status= 'Enable' GROUP BY id ORDER BY id DESC 

In collection factory i have below code

$collection = $this->collectionFactory->create()
           ->addFieldToFilter('id', $id)
           ->addFieldToFilter('status', 'Enable')
           ->setGroupBy('posts_id') // I tried to use group() as well
           ->setOrder('id','desc');
       

How to set Group By on above collection?

I have followed Magento 2 use group by in collection

I need to run below query

SELECT * FROM my_table WHERE status= 'Enable' GROUP BY id ORDER BY id DESC 

In collection factory i have below code

$collection = $this->collectionFactory->create()
           ->addFieldToFilter('id', $id)
           ->addFieldToFilter('status', 'Enable')
           ->setGroupBy('posts_id') // I tried to use group() as well
           ->setOrder('id','desc');
       

How to set Group By on above collection?

I have followed Magento 2 use group by in collection

I haveneed to run below query

SELECT * FROM my_table WHERE status= 'Enable' GROUP BY id ORDER BY id DESC

app\code\Custom\Module\Block\MyBlock\Posts.php

SELECT * FROM my_table WHERE status= 'Enable' GROUP BY id ORDER BY id DESC 

In collection factroyfactory i have below code

$collection = $this->collectionFactory->create()
           ->addFieldToFilter('id', $id)
           ->addFieldToFilter('status', 'Enable')
           ->setGroupBy('posts_id') // I tried to use group() as well
           ->setOrder('id','desc');
       

How to set Group By on above onescollection?

I have followed Magento 2 use group by in collection

I have below query

SELECT * FROM my_table WHERE status= 'Enable' GROUP BY id ORDER BY id DESC

app\code\Custom\Module\Block\MyBlock\Posts.php

In collection factroy i have below code

$collection = $this->collectionFactory->create()
           ->addFieldToFilter('id', $id)
           ->addFieldToFilter('status', 'Enable')
           ->setGroupBy('posts_id') // I tried to use group() as well
           ->setOrder('id','desc');
       

How to set Group By on above ones?

I have followed Magento 2 use group by in collection

I need to run below query

SELECT * FROM my_table WHERE status= 'Enable' GROUP BY id ORDER BY id DESC 

In collection factory i have below code

$collection = $this->collectionFactory->create()
           ->addFieldToFilter('id', $id)
           ->addFieldToFilter('status', 'Enable')
           ->setGroupBy('posts_id') // I tried to use group() as well
           ->setOrder('id','desc');
       

How to set Group By on above collection?

Source Link
Jackson
  • 10k
  • 31
  • 131
  • 217

Magento 2 How to set group by in Collection query?

I have followed Magento 2 use group by in collection

I have below query

SELECT * FROM my_table WHERE status= 'Enable' GROUP BY id ORDER BY id DESC

app\code\Custom\Module\Block\MyBlock\Posts.php

In collection factroy i have below code

$collection = $this->collectionFactory->create()
           ->addFieldToFilter('id', $id)
           ->addFieldToFilter('status', 'Enable')
           ->setGroupBy('posts_id') // I tried to use group() as well
           ->setOrder('id','desc');
       

How to set Group By on above ones?