SELECT `temp`.* FROM ((SELECT `support_tickets`.*, `support_replies`.`date_added` AS `last_reply_date`, `support_departments`.`name` AS `department_name`, `support_departments`.`company_id`, IF(support_replies.staff_id IS NULL, IF(support_tickets.email IS NULL, ?, ?), ?) AS `last_reply_by`, IF(support_replies.staff_id IS NULL, contacts.first_name, staff.first_name) AS `last_reply_first_name`, IF(support_replies.staff_id IS NULL, contacts.last_name, staff.last_name) AS `last_reply_last_name`, IF(support_replies.staff_id IS NULL, IFNULL(support_tickets.email, ?), ?) AS `last_reply_email` FROM `support_tickets` INNER JOIN `support_departments` ON `support_departments`.`company_id` = ? AND `support_departments`.`id` = `support_tickets`.`department_id` INNER JOIN `support_replies` ON `support_replies`.`type` = ? AND `support_replies`.`ticket_id` = `support_tickets`.`id` LEFT JOIN `clients` ON `clients`.`id` = `support_tickets`.`client_id` LEFT JOIN `contacts` ON `contacts`.`contact_type` = ? AND `contacts`.`client_id` = `clients`.`id` LEFT JOIN `staff` ON `staff`.`id` = `support_replies`.`staff_id` WHERE `support_tickets`.`status` = ? AND (`support_tickets`.`staff_id` = ? OR `support_tickets`.`department_id` IN (?,?)) ORDER BY `last_reply_date` DESC)) AS `temp` GROUP BY `temp`.`id`Array ( [0] => client [1] => email [2] => staff [3] => [4] => [5] => 1 [6] => reply [7] => primary [8] => open [9] => 1 [10] => 1 [11] => 2 )