⑴ 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();