有两个表 
表A存储用户数据  [ID,UserName,Hot] ,Hot是int,用户的热门指数,值越大越热 
表B存储用户所有用户发表的文章 [ID,UserID,Title,Content,Hot] ,Hot是int,文章的热门指数,值越大越热 我想找出热门数值在前8的用户的名称

这8位用户各自的热门指数在前8的文章 现在没有用存储过程,是在aspx里这么做的1步:SELECT TOP ID,UserName FROM A ORDER BY Hot Desc
2步:DataReader.Read() 
    userID1=DataReader["ID"]     DataReader.Read() 
    userID2=DataReader["ID"] 
    后面省略... 3步:SELECT TOP 8 ID,Title FROM B WHERE UserID=userID1 ORDER BY Hot DESC
SELECT TOP 8 ID,Title FROM B WHERE UserID=userID2 ORDER BY Hot DESC
SELECT TOP 8 ID,Title FROM B WHERE UserID=userID3 ORDER BY Hot DESC
后面省略... 现在我想把这些内容都放在一个存储过程里来完成,请问这个过程该如何写?谢谢大家

解决方案 »

  1.   

    --我想找出热门数值在前8的用户的名称 
    select top 8 ID,UserName,Hot from A order by hot desc--这8位用户各自的热门指数在前8的文章  
    select * from (select * from B where userid in (select top 8 ID from A order by hot desc)) m
    where hot in 
    (
    select top 8 hot from ((select * from B where userid in (select top 8 ID from A order by hot desc))) n where n.UserID = m.UserID order by n.hot desc
    )
      

  2.   

    --不知道你是单独显示,还是联合显示。
    create procedure my_proc
    as
    begin
      --单独各自的显示
      --我想找出热门数值在前8的用户的名称 
      select top 8 ID,UserName,Hot from A order by hot desc
      --这8位用户各自的热门指数在前8的文章  
      select * from (select * from B where userid in (select top 8 ID from A order by hot desc)) m
      where hot in 
      (
        select top 8 hot from (select * from B where userid in (select top 8 ID from A order by hot desc)) n where n.UserID = m.UserID order by n.hot desc
      )  --合起来显示,A表显示ID,UserName,Hot,B表显示UserID,Title,Hot
      select top 8 ID,UserName,Hot from A order by hot desc
      union all
      select UserID as ID,Title username,Hot from (select * from B where userid in (select top 8 ID from A order by hot desc)) m
      where hot in 
      (
        select top 8 hot from (select * from B where userid in (select top 8 ID from A order by hot desc)) n where n.UserID = m.UserID order by n.hot desc
      )
      order by ID , hot desc
    end
    go
      

  3.   

    Create proc UPG
    as
    select top 8 U.ID '用户序号',U.UserName '用户名',U.hot '用户热门指数',P.ID '文章序号',P.Title '文章标题',P.Content,P.hot '文章热门指数'  from uses u left join prog p on u.ID=p.userID  
    order by U.hot desc,P.hot desc