Есть 2 таблицы :
News и
NewsLikes
`News` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(80) CHARACTER SET utf8mb4 DEFAULT NULL,
`description` varchar(600) CHARACTER SET utf8mb4 DEFAULT NULL,
`imageIds` json DEFAULT NULL,
`views` int(11) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`userId` int(11) DEFAULT NULL,
`status` tinyint(4) NOT NULL DEFAULT '0',
`data` longblob,
PRIMARY KEY (`id`),
`NewsLikes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) DEFAULT NULL,
`newsId` int(11) NOT NULL,
`status` tinyint(4) NOT NULL DEFAULT '0',
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
Тестовые данные News
Тестовые данные Likes
Нужно написать такой sql запрос, чтобы результатом был такой вывод
На скриншоте выше выводятся строки за сегодня, вчера, позавчера и т.д (Т.е новость дня за сегодня, вчера и позавчера)
Нет дубликатов по полю created или likesCount.
В sql я не силен, поэтому чтобы получить такой вывод написал огромного монстра, но я уверен,что есть какое-то элегантное короткое решение, неравнодушных прошу помочь мне с написанием более короткого решения и более эффективного по скорости выполнения. Заранее спасибо за любую помощь, подсказку и наводку
P.S мой запрос
SELECT o.id, o.likes as likesCount, o.created, o.views, o.title, imageIds FROM
(SELECT o2.id, o2.created, o2.views, o2.title, o2.imageIds, IFNULL(o2.likes, 0) likes FROM
(SELECT max(id) as id FROM (SELECT id, created, likes FROM News wi LEFT JOIN (SELECT COUNT(NewsLikes.id) as likes, `newsId` FROM `NewsLikes` GROUP BY `newsId`) likesJoin ON wi.id = likesJoin.`newsId`) tl GROUP BY DATE_FORMAT(created, '%Y%m%d'), likes) tlg
JOIN (SELECT id,created,views,title, imageIds, IFNULL(likes,0) as likes FROM News o1 LEFT JOIN (SELECT COUNT(NewsLikes.id) as likes, `newsId` FROM `NewsLikes` GROUP BY `newsId`) likesJoin ON o1.id = likesJoin.`newsId`) o2 ON tlg.id = o2.id) o
LEFT JOIN
(SELECT o2.id, o2.created, o2.views, IFNULL(o2.likes, 0) likes FROM
(SELECT max(id) as id FROM (SELECT id, created, likes FROM News wi LEFT JOIN (SELECT COUNT(NewsLikes.id) as likes, `newsId` FROM `NewsLikes` GROUP BY `newsId`) likesJoin ON wi.id = likesJoin.`newsId`) tl GROUP BY DATE_FORMAT(created, '%Y%m%d'), likes) tlg
JOIN (SELECT id,created,views,title, IFNULL(likes,0) as likes FROM News o1 LEFT JOIN (SELECT COUNT(NewsLikes.id) as likes, `newsId` FROM `NewsLikes` GROUP BY `newsId`) likesJoin ON o1.id = likesJoin.`newsId`) o2 ON tlg.id = o2.id) b
ON DATE_FORMAT(o.created, '%Y%m%d') = DATE_FORMAT(b.created, '%Y%m%d')
AND (o.likes < b.likes)
WHERE b.created IS NULL
ORDER BY o.created DESC
LIMIT 100 OFFSET 0