I have below query. How to convert it in to Magento format?
SELECT GROUP_CONCAT(id) as ids FROM table
WHERE YEAR(created_at) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(created_at) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Try following :
$resource = $this->_objectManager->create('\Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION); $sql = $connection->select() ->from('table_name', array('ids' => new \Zend_Db_Expr('GROUP_CONCAT(id)'))) ->where('YEAR(created_at) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)') ->where('MONTH(created_at) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)'); $data = $connection->fetchAll($sql); print_r($data);