不需要用session统计。 数据库中有数据就直接用数据库中的数据进行统计就可以了。 declare @table table (rid int,userid int,第一次登录时间 datetime,最后一次登录时间 datetime) insert into @table select 1,1,'2010-01-12','2010-01-12' union all select 2,1,'2010-01-12','2010-01-12' union all select 3,2,'2010-01-12','2010-01-12'SELECT DISTINCT userid , 第一次登录时间 , 最后一次登录时间 , 登录次数 = ( SELECT COUNT(*) FROM @table WHERE userid = b.userid ) FROM @table b/* userid 第一次登录时间 最后一次登录时间 登录次数 ----------- ----------------------- ----------------------- ----------- 1 2010-01-12 00:00:00.000 2010-01-12 00:00:00.000 2 2 2010-01-12 00:00:00.000 2010-01-12 00:00:00.000 1 */
-- 做个存储过程,来记录用户登录的信息! create table user_rate(userid int,user_count int,start datetime,end datetime) gocreate proc sp_getusercount(@userid int) as begin if exists (select top 1 1 from user_rate where userid = @userid) begin update user_rate set user_count = user_count + 1 set end = getdate() end else begin insert into user_rate select @userid,1,getdate(),getdate() end end go
数据库中有数据就直接用数据库中的数据进行统计就可以了。
declare @table table
(rid int,userid int,第一次登录时间 datetime,最后一次登录时间 datetime)
insert into @table
select 1,1,'2010-01-12','2010-01-12' union all
select 2,1,'2010-01-12','2010-01-12' union all
select 3,2,'2010-01-12','2010-01-12'SELECT DISTINCT
userid ,
第一次登录时间 ,
最后一次登录时间 ,
登录次数 = ( SELECT COUNT(*)
FROM @table
WHERE userid = b.userid
)
FROM @table b/*
userid 第一次登录时间 最后一次登录时间 登录次数
----------- ----------------------- ----------------------- -----------
1 2010-01-12 00:00:00.000 2010-01-12 00:00:00.000 2
2 2010-01-12 00:00:00.000 2010-01-12 00:00:00.000 1
*/
-- 做个存储过程,来记录用户登录的信息!
create table user_rate(userid int,user_count int,start datetime,end datetime)
gocreate proc sp_getusercount(@userid int)
as
begin
if exists (select top 1 1 from user_rate where userid = @userid)
begin
update user_rate
set user_count = user_count + 1
set end = getdate()
end
else
begin
insert into user_rate
select @userid,1,getdate(),getdate()
end
end
go