· 8 years ago · Mar 07, 2018, 04:12 PM
1SELECT writings.id, writings.title, writings.excerpt, writings.description,
2 writings.body, writings.created_on, writings.updated_on, writings.created_by,
3 writings.updated_by, writings.featured, writings.views
4FROM writings
5LEFT JOIN comments ON (comments.commentable_id = writings.id)
6WHERE comments.commentable_type = "Writing"
7GROUP BY commentable_id
8
9---------
10
11DROP TABLE IF EXISTS `comments`;
12CREATE TABLE `comments` (
13 `id` int(11) NOT NULL auto_increment,
14 `title` varchar(50) default '',
15 `comment` varchar(255) default '',
16 `created_at` datetime NOT NULL,
17 `commentable_id` int(11) NOT NULL default '0',
18 `commentable_type` varchar(15) NOT NULL default '',
19 `user_id` int(11) NOT NULL default '0',
20 PRIMARY KEY (`id`),
21 KEY `fk_comments_user` (`user_id`)
22) ENGINE=InnoDB DEFAULT CHARSET=latin1;
23
24
25DROP TABLE IF EXISTS `writings`;
26CREATE TABLE `writings` (
27 `id` int(11) NOT NULL auto_increment,
28 `title` varchar(255) default NULL,
29 `excerpt` varchar(255) default NULL,
30 `description` text,
31 `body` text,
32 `created_on` datetime default NULL,
33 `updated_on` datetime default NULL,
34 `created_by` int(11) default NULL,
35 `updated_by` int(11) default NULL,
36 `featured` tinyint(1) default NULL,
37 `views` int(11) default NULL,
38 PRIMARY KEY (`id`)
39) ENGINE=InnoDB DEFAULT CHARSET=latin1;