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

             利用游标和表变量

  • 相关阅读:
    阿里云nginx创建多站点
    linux 卸载php mysql apache
    centos php环境搭建
    jquery simple modal
    nodejs 安装express
    nodejs fs.open
    nodejs supervisor
    nodejs 运行
    nodejs shell
    PHP array_pad()
  • 原文地址:https://www.cnblogs.com/ajun/p/2184847.html
Copyright © 2011-2022 走看看