功能要求 :有两张表a,b
a:
bookid(identity 1,1,primaty key) name(varchar(50)
1 xml
2 c++
3 sql
4 oracal
b:
id(identity 1,1,primary key) bookId(a的外键) userId content(text) time(DateTime)
1 1 d dfgjkkghh 2006-9-29
2 1 d jkkl;gkl; 2006-9-29
3 2 a dfdfdhghdk 2006-9-28
4 2 65676986986 2006-5-9
5 3 ggghghgh 2006-9-10
6 4 ghjkjkl 2006-9-25
7 1 rsggah 2006-9-16
现在要选出根据时间降序排列,排名前三的无重复的记录,其中要包括bookid name content time 的信息
这个SQL语句如何写啊?
答案:

select rowid = identity(int,1,1),a.name,a.bookid,b.content,b.time into #
from a as a
left join b as b on a.bookid=b.bookId
order by b.time desc

select TOP 3 name,bookid,content,time
from # d
where not exists(select 1 from # where d.name = name and rowid < d.rowid )
drop table #
1425

被折叠的 条评论
为什么被折叠?



