Show article title, post, last reply time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.