给以前大学的同学解决了一个小问题,sql的一个普遍的用法

功能要求 :有两张表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  #
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值