急求让同一用户发布的N条信息在一个页面只能显示3条的SQL或存储过程!
要求:例如在一个论坛上,某一用户回帖的N条信息中,在一个页面一次只能显示最新3条记录,请问这样的SQL或存储过程怎么写!谢谢!
------解决方案--------------------DECLARE @Test TABLE (ID int, Content int)
INSERT @Test SELECT 1, 11 UNION ALL
SELECT 1, 12 UNION ALL
SELECT 1, 12 UNION ALL
SELECT 1, 13 UNION ALL
SELECT 1, 14 UNION ALL
SELECT 2, 21 UNION ALL
SELECT 2, 22 UNION ALL
SELECT 2, 23 UNION ALL
SELECT 2, 24 UNION ALL
SELECT 2, 25 UNION ALL
SELECT 3, 31 UNION ALL
SELECT 3, 32 UNION ALL
SELECT 3, 33
SELECT * FROM @Test a WHERE Content IN (SELECT TOP 3 Content FROM @Test WHERE ID = a.ID) ORDER BY ID, Content