Skip to content

Instantly share code, notes, and snippets.

@ABM-Dan
Last active January 27, 2016 22:27
Show Gist options
  • Save ABM-Dan/108239bb0c8afe1b7579 to your computer and use it in GitHub Desktop.
Save ABM-Dan/108239bb0c8afe1b7579 to your computer and use it in GitHub Desktop.
<?php
// Trying to emulate
// SELECT l.*, MAX(s.sale_ts) FROM location AS `l` LEFT JOIN sales AS `s` ON l.id = s.location_id GROUP BY(s.location_id);
// But the group selects the last result.
$qb = $this->createQueryBuilder('l')
->leftJoin('l.sales', 's')
->where('l.is_active = 1')
->orderBy('l.id', 'ASC')
->addOrderBy('s.sale_ts', 'DESC')
->groupBy('l.id');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment