⑴ sql 如何查询日期在一定范围内的数据
select * from 表 where 日期字段>='开始日期' and 日期字段<='截止日期' and convert(char(8),日期字段,108)>='开始时间' and convert(char(8),日期字段,108)<='截止时间'。
SELECT * FROM 表明 WHERE 日期字段名 BETWEEN '20130101' AND '20130130'。
例如:
select * from tb1 where dDate>='2010-11-05' and dDate<='2010-11-15'
and convert(char(8),dDate,108)>='8:00:00' and convert(char(8),dDate,108)<='9:00:00'.
select * from table1where year(d)=2010 and month(d)=7 and day(d) between 1 and 31
and (Datepart(hour,d)>=22 or Datepart(hour,d)<6)
(1)sql统计每30天内的数据扩展阅读:
SQL查询日期:
今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0
昨天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=1
7天内的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=7
30天内的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=30
本月的所有数据:select * from 表名 where DateDiff(mm,datetime类型字段,getdate())=0
本年的所有数据:select * from 表名 where DateDiff(yy,datetime类型字段,getdate())=0
参考资料:SQL_网络
⑵ SQL查询最近三个月的数据(查询最近几天,几
1、创建测试表,create table test_date2(id number, v_date date);
⑶ [MSsql] 如何查询近30天的数据
DATEADD (datepart ,number,date)函数 在日期中添加或减去指定的时间间隔。datepart指的是时间的那一部分,比如年number指的是时间间隔,可以用负值date指的是从那个日期开始添加或减去时间间隔
select dateadd(day,-30,getdate())
⑷ mysql 查询30天内每天的记录总数!
if
(mysql_connect('localhost','root','root')){
$query
=
"select
count(aid)
as
ct
from
table
group
by
aid";//
从数据库中读取数据
,count出来加了别名ct用来获取数据时标示字段名用
if
($result=@mysql_query($query)){
while($rows=@mysql_fetch_array($result))
{
$rows[ct];//这里循环处理每一行ct字段的值
}
mysql_free_result($result);
⑸ 求统计当天及当天和30天内订单数和总金额的SQL语句
oracle: select sum(orders),sum(total) from table where orderdate between sysdate-30 and sysdate;
sql: select sum(orders),sum(total) from table where orderdate between dateadd(dd,-30,getdate()) and getdate();