表logevent
字段logid 主键
ToUserId
LogTypeId
LogTime
UserId
Notes表fuser
id主键
FriendId
UserId要求查询表logevent useid在表fuser中的,并且根据logtime 排序,把logtime大的分组排列。
我写的如下。现在问题怎么实现分页?
select top 10 userid from logevent where logid in(
select max(logid) from logevent group by userid
)
and userid in (
select friendid as userid from fuser where userid='38'
)
and ( touserid='38' or touserid is null )order by logtime desc
解决方案 »
- 求一个复杂的百分比的SQL语句写法,最好能写成存储过程
- 请教带有IF判断的SQL语句
- select * from 服务器名.数据库名.dbo.表名
- 两个表通过外键连接,在查询时可否直接获取所有字段内容而不必通过在查询语句中写
- 用SQLDMO进行SQL SERVER数据库备份,结果没生成备份文件,急请高手帮忙.
- sqlserver 2008 R2 本地无法登陆-------高分求助!!
- 取出标题不同的文章
- 数据库select问题!!
- 好象是用户数量受限制,出现如下提示,使我没办法在另外的机器连接,但我不是设为单用户模式,不只为什么
- 关于Sql Server的链接服务器功能(一定给分)
- sql group by分组排序问题
- 求助,关于数据库更新语句的写法
create procedure hahaha
@sqlstr nvarchar(4000), --查询字符串
@currentpage int, --第N页
@pagesize int --每页行数
as
set nocount on
declare @P1 int, --P1是游标的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off
--测试: 2 代表第二页 ,10 每页10条
exec hahaha '任何条件的SQL语句',2,10