SELECT o.`id`, o.`image`, o.`name`, o.`alias`, o.`image`, date_format(o.`updated`, '%m') as `mon`, date_format(o.`updated`, '%e') as `day`, date_format(o.`updated`, '%Y') as `year`, date_format(o.`updated`, '%H:%i') as `time`, CASE WHEN i.`id` IS NOT NULL THEN count(*) ELSE 0 END as count_images FROM `dbdrive_gallery` as o LEFT JOIN `dbdrive_images` as i ON i.`pid` = o.`id` WHERE o.`tree_id` = '40' AND o.`active` = 1 ORDER BY o.`ord` LIMIT 3