SELECT `ui`.`id` AS `image_id`,
`ui`.`user_id` AS `user_id`,
`ui`.`create_date` AS `image_create_date`,
`ui`.`name` AS `image_name`,
`ui`.`title` AS `image_title`,
`ui`.`descr` AS `image_descr`,
`ui`.`views` AS `image_views`,
`ui`.`plus` AS `image_plus`,
`ui`.`minus` AS `image_minus`,
`bu`.`first_name` AS `user_first_name`,
`bu`.`last_name` AS `user_last_name`,
`bu`.`photo` AS `user_photo`,
COUNT(`c`.`id`) AS `com_cnt`
FROM `user_images` AS `ui`
LEFT JOIN `bpl_users` AS `bu` ON `bu`.`id` = `ui`.`user_id`
LEFT JOIN `comments` AS `c` ON `c`.`image_id` = `ui`.`id`
GROUP BY `ui`.`id`
ORDER BY `ui`.`create_date` DESC LIMIT 9
CREATE INDEX `image_id_idx` ON `comments` (`image_id`);
SELECT
user_images.id AS image_id, user_images.user_id AS user_id,
user_images.create_date AS image_create_date, user_images.name AS image_name,
user_images.title AS image_title, user_images.descr AS image_descr,
user_images.views AS image_views, user_images.plus AS image_plus,
user_images.minus AS image_minus,
bpl_users.first_name AS user_first_name, bpl_users.last_name AS user_last_name,
bpl_users.photo AS user_photo,
(SELECT COUNT(*) FROM `comments` WHERE `image_id`=`user_images`.`id` ) AS quantity_comments
FROM `user_images` INNER JOIN `bpl_users`
ON `user_images`.`user_id`=`bpl_users`.`id`
ORDER BY `user_images`.`create_date` DESC LIMIT 9