首页 > 代码库 > SQL语句关于时间的查询小心得,希望大家给点意见
SQL语句关于时间的查询小心得,希望大家给点意见
查询本月信息:
Select * FROM T_Users Where datediff(month,RegisterTime,getdate())=0
昨天的信息:
SELECT * FROM T_Users where LastLoginTime>DATEADD(DAY,-2,GETDATE()) AND LastLoginTime<GETDATE()
SELECT SUM([DetailMoney]) FROM [dbo].[T_BuyDetails] INNER JOIN [dbo].[T_Users] ON [CommenderID]=[UserID]
WHERE datediff(month,[DateTime],getdate())=0
--本月
SELECT SUM([DetailMoney]) FROM [dbo].[T_BuyDetails]
INNER JOIN [dbo].[T_Users] ON [CommenderID]=[UserID]
WHERE CONVERT( char(10),[DateTime],120)>CONVERT(char(10),DATEADD(DAY,-2,GETDATE()),120) AND CONVERT( char(10),[DateTime],120)<CONVERT( char(10),GETDATE(),120)
--昨日
SELECT SUM([DetailMoney]) FROM [dbo].[T_BuyDetails]
WHERE CONVERT( char(10),[DateTime],120)=CONVERT( char(10),GETDATE(),120)
--今日
SELECT SUM([DetailMoney]) FROM [dbo].[T_BuyDetails]
--总消费