表A
aid groupid, uid
1 1001 25
2 1001 26
3 1001 27
4 1001 28
5 1001 296 1002 31
7 1002 45
8 1002 27
9 1002 28
10 1002 2911 1003 25
12 1003 26
13 1003 31
14 1003 33
15 1003 46B表
bid content createon uid
101 文章1 时间1 25
102 文章2 时间34 25
103 文章3 时间12 26
104 文章4 时间4 25
105 文章5 时间5 26106 文章1 时间32 46
107 文章2 时间2 37
108 文章3 时间56 89
109 文章4 时间65 25
110 文章5 时间43 54111 文章1 时间11 32
112 文章2 时间89 45
113 文章3 时间3 25
114 文章4 时间78 25
115 文章5 时间44 11需要是 根据A表的groupid ,查找属于指定groupid 的每一个用户(uid) 最新发表的的一条文章
按时间desc排序 最终返回 用户的ID、文章等字段
还要页- -
select top(@top) uid, (select top 1 content from B where uid=a.uid order by createon desc) as content
from A where groupid=@groupid and uid not in
(
select top(@top(@size*(@index-1))) a.uid,(select top 1 createon from B where uid=a.uid) as time
from A a where a.groupid=1001 order by time
)
总觉得使用(select top 1 createon from B where uid=a.uid) as time
select top 1 content from B where uid=a.uid order by createon desc这样的写法 不怎么好
谁有更好的写法???比如groupid:10011001 28 它的文章 最新发表
1001 24 它的文章 第二新发表
1001 25 它的文章 第三新发表
1001 29 它的文章 第四新发表
aid groupid, uid
1 1001 25
2 1001 26
3 1001 27
4 1001 28
5 1001 296 1002 31
7 1002 45
8 1002 27
9 1002 28
10 1002 2911 1003 25
12 1003 26
13 1003 31
14 1003 33
15 1003 46B表
bid content createon uid
101 文章1 时间1 25
102 文章2 时间34 25
103 文章3 时间12 26
104 文章4 时间4 25
105 文章5 时间5 26106 文章1 时间32 46
107 文章2 时间2 37
108 文章3 时间56 89
109 文章4 时间65 25
110 文章5 时间43 54111 文章1 时间11 32
112 文章2 时间89 45
113 文章3 时间3 25
114 文章4 时间78 25
115 文章5 时间44 11需要是 根据A表的groupid ,查找属于指定groupid 的每一个用户(uid) 最新发表的的一条文章
按时间desc排序 最终返回 用户的ID、文章等字段
还要页- -
select top(@top) uid, (select top 1 content from B where uid=a.uid order by createon desc) as content
from A where groupid=@groupid and uid not in
(
select top(@top(@size*(@index-1))) a.uid,(select top 1 createon from B where uid=a.uid) as time
from A a where a.groupid=1001 order by time
)
总觉得使用(select top 1 createon from B where uid=a.uid) as time
select top 1 content from B where uid=a.uid order by createon desc这样的写法 不怎么好
谁有更好的写法???比如groupid:10011001 28 它的文章 最新发表
1001 24 它的文章 第二新发表
1001 25 它的文章 第三新发表
1001 29 它的文章 第四新发表
麻烦你们问sql问题,给出建表语句和插入测试数据的sql。
因为正常的前台操作绝对不会有一个人在相同的时间发表2分以上的文章,所以你拿下面的套吧CREATE TABLE [dbo].[UserInfo]
(
[UserID] [varchar](10) NULL,
[LoginDate] [varchar](10) NULL
)
insert into userInfo
select 'A', '2010.08.06' union all
select 'B', '2010.08.01' union all
select 'A', '2010.08.02' union all
select 'C', '2010.08.08' union all
select 'B', '2010.08.07' union all
select 'C', '2010.08.03'--结果
A 2010.08.06
B 2010.08.07
C 2010.08.08
FROM (
SELECT MAX(t0.LoginDate) AS [value], t0.UserID
FROM UserInfo AS t0 GROUP BY t0.UserID
) AS t1 CROSS JOIN UserInfo AS t2
WHERE t2.LoginDate = t1.value AND
t1.UserID = t2.UserID
ORDER BY t2.UserID asc