SELECT COUNT ( `h` . `id` ) AS `count` , `b` . `id` , `b` . `summary` , `b` . `view_state` FROM `mantis_bug_table` `b` JOIN `mantis_bug_history_table` `h` ON `h` . `bug_id` = `b` . `id` WHERE `b` . `status` < ? AND `project_id` IN (...) AND `b` . `id` IN ( SELECT `mantis_bug_table` . `id` FROM `mantis_bug_table` JOIN `mantis_project_table` ON `mantis_project_table` . `id` = `mantis_bug_table` . `project_id` WHERE `mantis_project_table` . `enabled` = ? AND ( `mantis_bug_table` . `project_id` IN (...) ) ) GROUP BY `h` . `bug_id` , `b` . `id` , `b` . `summary` , `b` . `last_updated` , `b` . `view_state` ORDER BY `count` DESC , `b` . `last_updated` DESC 