我正在做一个会员系统,允许会员赚取和使用积分。其中一些积分也有有效期。
目前我的系统有3个数据库表。
1)包含个人详细信息的成员表,并有一列用于存储成员的总积分。
2)用于存储积分收入和到期日期(如果有)的交易表
3)积分使用表,用于存储使用了多少积分的记录,可能用于从我的网站购买商品(每行每次使用)
我很难确定哪些积分在过期之前正在使用,或者它只是过期了。
所以我希望我能在这里得到一些建议或建议。或者是我的数据库表设计有什么问题导致了这个问题?
更新:一旦会员获得或使用积分,保持更新会员表中的总点数列是好的吗,这会使系统做额外的工作吗?
发布于 2016-01-02 18:35:45
我不确定我确切地理解你需要什么,但我想出了这样的东西:
create table UserPoints(
Id int not null identity( 1, 1 ),
UserId int not null,
Points int not null,
[Date] datetime2( 3 ) not null,
ExpireDate datetime2( 3 ) null
)
Go
-- + for earning points
-- - for loosing points
Insert into UserPoints Values
( 1,10,getdate(), null ),
( 1,10,dateadd(hour,1,getdate()), null ),
( 1,100,dateadd(month,2,getdate()), '2016-04-02' ),
( 1,-15,dateadd(month,3,getdate()), null ),
( 1,30,dateadd(month,4,getdate()), null )
Go
select * from UserPoints order by [date]
--declare @checkDate datetime2(3) = '2016-04-02'
--declare @checkDate datetime2(3) = '2016-04-02 10:00'
declare @checkDate datetime2(3) = '2016-04-03'
select
sum( points ) as 'UserPointsAtSpecificDate'
from
UserPoints
where
( UserId = 1 )
and ( [date] <= @checkDate )
and ( isnull( expireDate, '9999-12-31' ) >= @checkDate )
https://stackoverflow.com/questions/34564110
复制相似问题