Skip to content

Instantly share code, notes, and snippets.

@settermjd
Last active August 29, 2015 14:18
Show Gist options
  • Save settermjd/3360ccabb2b761a6ee11 to your computer and use it in GitHub Desktop.
Save settermjd/3360ccabb2b761a6ee11 to your computer and use it in GitHub Desktop.
Joining equal values​​, that were returned from a database
/**
* The following code builds this query:
* SELECT `amount`
* , GROUP_CONCAT(`Name` SEPARATOR ', ') AS `name`
* FROM `table`
* GROUP BY `amount`
* ORDER BY `amount` DESC
* LIMIT 0, 3
*/
public function getSome()
{
$select = $this->tableGateway->getSql()->select();
$select->columns([
'amount',
'name' => new \Zend\Db\Sql\Expression("GROUP_CONCAT(Name SEPARATOR ', ')")
])
->order('amount DESC')
->group('amount')
->limit(3);
return $this->tableGateway->selectWith($select);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment