I'm porting an application from ZF1 to ZF2 and as part of that I have to rewrite our database mappers.
I'm struggling with this SQL statement:
SELECT full_name, GROUP_CONCAT(value)
FROM (
SELECT full_name, value
FROM my_table
ORDER BY id DESC
) as subtable
GROUP BY full_name
ORDER BY full_name DESC;
The underlying problem I'm trying to solve is that I need to order the results of the sub query before running GROUP_CONCAT
and I need it to work for both MySQL and Sqlite. In MySQL I could simply specify the order within the GROUP_CONCAT
function, but this is not possible with Sqlite so I need the sub query for it to be compatible with both MySQL and Sqlite.
In ZF1 I could do:
$fromSql = $db->select()
->from('my_table', array('full_name', 'value'))
->order('id DESC');
$sql = $db->select()
->from(array(
'subtable' => new Zend_Db_Expr('(' . $fromSql . ')')
), array(
'full_name' => 'full_name',
'value' => new Zend_Db_Expr('GROUP_CONCAT(subtable.value)'),
)
)
->group('full_name')
->order('full_name DESC');
However, using a sub query in the from clause doesn't seem possible with ZF2. Is there some work around for this?