I am trying to build a sql query with ZendFW, but I cannot make it function the way I want (or even work). This is a query that works, which I am trying to build with zend_db select ()
SELECT tc.trip_title, td.ID, td.trip_id,
(SELECT count(*) FROM 'trips_invites' ti
WHERE ti.destination_id=td.ID AND ti.accepted ='NR') AS "pending_invites"
FROM `trips_current` AS `tc`, `trips_data` AS `td`
WHERE (tc.ID=td.trip_id) AND (tc.creator_id = '1')
ORDER BY `trip_id` ASC
I can't figure out how to properly get this subquery there, and nothing I try seems to work.
Any help would be greatly appreciated!
Thank!
Edit / Response: If anyone has the same problem, based on the suggestion below, I will reprocess the request as follows:
SELECT `tc`.`trip_title`, `td`.`ID`, `td`.`trip_id`, count(TI.ID)
FROM `trips_current` AS `tc`
INNER JOIN `trips_data` AS `td` ON td.trip_id = tc.ID
LEFT JOIN trips_invites AS TI ON ti.destination_id = td.id
WHERE tc.creator_id = 1 AND ti.accepted='NR'
GROUP BY td.id
ORDER BY `trip_id` ASC
which with ZendFW I created as follows:
$select = $this->dblink->select()
->from(array('tc' => 'trips_current'),
array('trip_title'))
->join(array('td' => 'trips_data'),
'td.trip_id = tc.id',
array('ID','trip_id'))
->joinLeft(array('ti'=>'trips_invites'),
'ti.destination_id = td.id',
array('COUNT(ti.id)'))
->where('tc.creator_id =?',1)
->group('td.id')
->order('trip_id');