zoukankan      html  css  js  c++  java
  • 面试题,找出每个产品的最新五个产品,还有其它方法吗 —— 游标加表变量

    表t_c 和t_p如上图

    select * from t_c
    select * from t_p

    查询方法一:

               select * from (select row_number() over(partition by cid order by cdate desc) as rowid,* from t_p) T where T.rowid<=5

               使用sql2005的特性 row_number() over()

    方法二:

               declare @temp table(id int,cid int,cdate datetime);
           declare @id int;
        declare cur cursor for
           select cid from t_c
        open cur
        fetch next from cur Into @id
        while @@fetch_status = 0
        begin
             insert into @temp
             select top 5 * from t_p where cid = @id order by cdate desc
             fetch next from cur into @id
        end
        close cur
        deallocate cur
        select * from @temp

             利用游标和表变量

  • 相关阅读:
    Redis开发与运维:SDS
    Redis开发与运维:数据迁移
    我的2019上半年
    C# 并发编程
    经典排序算法 — C# 版(上)
    图解 -- 树的汇总
    图解--队列、并发队列
    栈到CLR
    我们的数组
    算法复杂度
  • 原文地址:https://www.cnblogs.com/ajun/p/2184847.html
Copyright © 2011-2022 走看看