· 8 years ago · Mar 23, 2018, 05:52 PM
1CREATE TABLE IF NOT EXISTS `posts` (
2 `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
3 `user` int(11) UNSIGNED NOT NULL,
4 `titulo` text,
5 `data` datetime NOT NULL,
6 PRIMARY KEY (`id`)
7) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ;
8
9CREATE TABLE IF NOT EXISTS `share` (
10 `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
11 `user` int(11) UNSIGNED NOT NULL,
12 `share_post_id` int(11) UNSIGNED NOT NULL,
13 `data` datetime NOT NULL,
14 PRIMARY KEY (`id`)
15) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ;
16
17select c.nome, c.user, p.user, p.id, p.data, p.titulo, pp.foto, count(DISTINCT likes.user) as likes_count, count(distinct comentarios.id) as comentarios_count, count(DISTINCT l2.user) as count2 from posts p
18join cadastro c on p.user=c.id
19left join profile_picture pp on p.user = pp.user
20left join likes on likes.post = p.id
21left join comentarios on comentarios.foto = p.id and comentarios.delete = 0
22left join likes l2 on l2.post = p.id and l2.user = ?
23group by p.id
24order by p.id desc limit ?