Sql Server中询问当天,如今八日,本周,前段时期,这段时间5个月,明年的数目的sql语句

by admin on 2019年9月5日

当天:

   mysql查询当天的所有信息:

select * from T_news where datediff(day,addtime,getdate())=0

  代码如下

最近三天:

  select * from test where year(regdate)=year(now()) and
month(regdate)=month(now()) and day(regdate)=day(now())

select * from T_news where datediff(day,addtime,getdate())<= 2 and
datediff(day,addtime,getdate())>= 0

  这个有一些繁琐,还有简单的写法:

本周:

  代码如下

select * from T_news WHERE (DATEPART(wk, addtime) = DATEPART(wk,
GETDATE())) AND (DATEPART(yy, addtime) = DATEPART(yy, GETDATE()))

  select * from table where date(regdate) = curdate();

注意:此时不能用 datediff 差值为7,因为,datediff只表示间隔数

  另一种写法没测试过

本月:

  查询当天的记录

select * from T_news WHERE (DATEPART(yy, addtime) = DATEPART(yy,
GETDATE())) AND (DATEPART(mm, addtime) = DATEPART(mm, GETDATE()))

  代码如下

最近一个月:

  select * from hb_article_view where TO_DAYS(hb_AddTime) =
TO_DAYS(NOW())

select * from T_news WHERE (DATEPART(yy, addtime) = DATEPART(yy,
GETDATE())) 
AND ((DATEPART(mm, addtime) = DATEPART(mm, GETDATE())) OR
(31-DATEPART(DD,addtime)+DATEPART(DD, GETDATE()))<=31)

  date()函数获取日期部分, 扔掉时间部分,然后与当前日期比较即可

本季度:

  补充:本周、上周、本月、上个月份的数据

select * from T_news where DATEPART(qq, addtime) = DATEPART(qq,
GETDATE()) and DATEPART(yy, addtime) = DATEPART(yy, GETDATE())

  查询当前这周的数据

  代码如下

  SELECT name,submittime FROM enterprise WHERE
YEARWEEK(date_format(submittime,’%Y-%m-%d’)) = YEARWEEK(now());

  查询上周的数据

  代码如下

  SELECT name,submittime FROM enterprise WHERE
YEARWEEK(date_format(submittime,’%Y-%m-%d’)) = YEARWEEK(now())-1;

  查询当前月份的数据

  select name,submittime from enterprise where
date_format(submittime,’%Y-%m’)=date_format(now(),’%Y-%m’)

  查询距离当前现在6个月的数据

  代码如下

  select name,submittime from enterprise where submittime between
date_sub(now(),interval 6 month) and now();

  查询上个月的数据

  代码如下

  select name,submittime from enterprise where
date_format(submittime,’%Y-%m’)=date_format(DATE_SUB(curdate(),
INTERVAL 1 MONTH),’%Y-%m’)

  select * from `user` where DATE_FORMAT(pudate,’%Y%m’) =
DATE_FORMAT(CURDATE(),’%Y%m’) ;

  select * from user where
WEEKOFYEAR(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = WEEKOFYEAR(now())

  select *

  from user

  where MONTH(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = MONTH(now())

  select *

  from [user]

  where YEAR(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = YEAR(now())

  and MONTH(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = MONTH(now())

  select *

  from [user]

  where pudate between 上月最后一天

  and 下月第一天

  mysql查询多少秒内的数据

  代码如下

  SELECT count( * ) AS c, sum( if( logusertype =2, logusertype, 0 ) )
/2 AS a, sum( if( logusertype =3, logusertype, 0 ) ) /3 AS b

  FROM testlog WHERE UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(
logendtime )<=30

  查询30秒内记录的总数,loguser等于2的记录的总数和,和
loguser等于3的记录的总数.

  if( logusertype =2, logusertype, 0 ) 如果logusetype等于2
就在logusertype上累加,否则加0。

  sum( if( logusertype =2, logusertype, 0 ) )
把logusertype都累加起来。

  sum( if( logusertype =2, logusertype, 0 ) ) /2 AS a,
除以2是统计个数。

  UNIX_TIMESTAMP(NOW())计算当前时间的秒数,

  UNIX_TIMESTAMP( logendtime )计算logendtime的秒数

代码如下 select * from
test where year(regdate)=year(now()) and month(regdate)=month(now()) and
day(regdate)=day(now()) 这个有一些繁琐,…

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图