drop table if exists articles;
CREATE table articles (ID int auto_increment primary key,title varchar), Postuser varchar, postdate datetime,parent ID int references articles (ID));
INSERT INTO articles values
(NULL, ' first ' , ' Zhang San ', ' 1998-10-10 12:32:32 ', null),
(NULL, ' second ' , ' Zhang San ', ' 1998-10-10 12:34:32 ', null),
(NULL, ' first reply 1 ', ' John Doe ', ' 1998-10-10 12:35:32 ', 1),
(NULL, ' second reply 1 ', ' John Doe ', ' 1998-10-10 12:36:32 ', 2),
(NULL, ' first reply 2 ', ' Harry ', ' 1998-10-10 12:37:32 ', 1),
(NULL, ' first reply 3 ', ' John Doe ', ' 1998-10-10 12:38:32 ', 1),
(NULL, ' second reply 2 ', ' John Doe ', ' 1998-10-10 12:39:32 ', 2),
(NULL, ' first reply 4 ', ' Harry ', ' 1998-10-10 12:39:40 ', 1);
Here are two ways to get results in MySQL:
1.
Select A.title,a.postuser,
(select Max (postdate) from articles where parentid=a.id) reply
From articles a where a.parentid is null;
2.select E.title,e.postuser,ep.postdate from Articles E,
(select Max (postdate) postdate,
ParentID
From articles
where
ParentID is not NULL
Group by ParentID) EP
where ep.parentid=e.id
In Netezza, such as: The first way will be an error to rewrite, so in Netezza we want to use the second way to query
Show article title, post, last reply time