标签:
drop table if exists articles;
create table articles(id int auto_increment primary key,title varchar(50), postuser varchar(10), postdate datetime,parentid int references articles(id));
insert into articles values
(null,‘第一条‘,‘张三‘,‘1998-10-10 12:32:32‘,null),
(null,‘第二条‘,‘张三‘,‘1998-10-10 12:34:32‘,null),
(null,‘第一条回复1‘,‘李四‘,‘1998-10-10 12:35:32‘,1),
(null,‘第二条回复1‘,‘李四‘,‘1998-10-10 12:36:32‘,2),
(null,‘第一条回复2‘,‘王五‘,‘1998-10-10 12:37:32‘,1),
(null,‘第一条回复3‘,‘李四‘,‘1998-10-10 12:38:32‘,1),
(null,‘第二条回复2‘,‘李四‘,‘1998-10-10 12:39:32‘,2),
(null,‘第一条回复4‘,‘王五‘,‘1998-10-10 12:39:40‘,1);
在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
在Netezza 中如:第一种方式的就会报错让你重写,所以在Netezza中我们要使用第二种方式查询
标签:
原文地址:http://www.cnblogs.com/Devilwx/p/4628668.html