SELECT `a`.*, `b`.`code` as `rack_code`, `c`.`id` as `room_id`, `c`.`code` as `room_code`, count(distinct d.id) as count_documents
FROM `kbs__box` `a`
JOIN `kbs__rack` `b` ON `b`.`id` = `a`.`rack_id`
JOIN `kbs__room` `c` ON `c`.`id` = `b`.`room_id`
LEFT JOIN `kbs__document` `d` ON `d`.`box_id` = `a`.`id` AND `d`.`is_delete` = 0
WHERE `a`.`is_delete` =0
AND `a`.`rack_id` = '4'
GROUP BY `a`.`id`