sql時間段語句
① sql語句 時間段查詢 高分
假設開始的時間是begintiem,結束的時間是endtime
select * from recordtime where gettime>beginTime and gettime<endtime;
如:
select * from recordtime where gettime>'2009-11-3'and gettime<'2009-12-3';
而你begintime和endtime可以從界面取得然後傳到sql語句中,你試試看吧!
祝你成功。。。
人家是sqlserver,而你寫的是oracle,資料庫錯了!
② 按照時間段統計的sql語句
我只能大概說一個想法,
創建一個中間表,存放8點-14點 14點-20點 20點-2點 2點-8點 這樣的一個標題哪燃枝及具體的時間段,
然後用這個中間表與biao1根據時李敏間tm進行關聯,就可以把數據與時間段聯繫上。
然後按這個時間段進行段團了group by ,之後再用 pivot就可以出來了。
③ 關於時間段查詢的SQL語句應該怎麼寫
各個資料庫的時間函數不一樣,我以mysql舉例
now()
j就是當前時間,
now()
-
INTERVAL
XX
DAY
就是當前時間以前
XX天下
面先就是查詢昨天到8天以前的sql例子
----------------------------
SELECT
*
FROM
`users`
k
WHERE
k.`create_time`
BETWEEN
NOW()
-
INTERVAL
8
DAY
AND
NOW()
-
INTERVAL
1
DAY
④ sql對時間段判斷語句
如你的資料庫中記錄時間的項名是selldate(是其他的伍絕話,你改下就行)則:
1.你的歷橘悄資料庫如果是access用下面的語句
<%
dim
nowstartdate
nowenddate
nowstartdate=2009-01-15
nowenddate=2009-02-15
sql="select
*
from
你的表
where
selldate-#"&nowstartdate&"#>=0
and
selldate-#"&nowenddate&"#<=0"
%>
2.你的資料庫如果是mssql用下面的語句
<%
dim
nowstartdate
nowenddate
nowstartdate=2009-01-15
nowenddate=2009-02-15
sql="select
*
from
你的表
where
selldate-'"&nowstartdate&"'>=0
and
selldate-'"&nowenddate&"'<=0"
%>
或者
1.把日期當字元串來操作,其實日期和數字一樣,在sql語句中,都肢渣不能加'字元串',否則會出現語法錯誤
2.選擇某個日期中間的記錄可以用between
and
而不是用<
>
=等符號,而且日期之間也有講究,access資料庫的日期間用#日期#隔開,mssql用'單引號,如:
timestr類型為日期/時間
dim
datestr1,datestr2
datestr1="2006-8-10"
datestr2=date()
sql="select
*
from
table
where
timestr
between
#"&datestr1&"#
and
#"&datestr2&"#"
⑤ 時間段分組的SQL語句請教
select case when convert(char(2),lTime,108)>=0 and convert(char(2),lTime,108)<=1
then '00:00-01:00'
when convert(char(2),lTime,108)>=1 and convert(char(2),lTime,108)<=2
then '01:00-02:00'
when convert(char(2),lTime,108)>=2 and convert(char(2),lTime,108)<=3
then '02:00-03:00'
when convert(char(2),lTime,108)>=3 and convert(char(2),lTime,108)<=4
then '03:00-04:00'
when convert(char(2),lTime,108)>=4 and convert(char(2),lTime,108)<=5
then '04:00-05:00'
when convert(char(2),lTime,108)>=5 and convert(char(2),lTime,108)<=6
then '05:00-06:00'
when convert(char(2),lTime,108)>=6 and convert(char(2),lTime,108)<=7
then '06:00-07:00'輪核源
when convert(char(2),lTime,108)>=7 and convert(char(2),lTime,108)<=8
then '臘態07:00-08:00'
when convert(char(2),lTime,108)>=8 and convert(char(2),lTime,108)<=9
then '08:00-09:00'
when convert(char(2),lTime,108)>=9 and convert(char(2),lTime,108)<=10
then '09:00-10:00'
when convert(char(2),lTime,108)>=10 and convert(char(2),lTime,108)<=11
then '10:00-11:00'
when convert(char(2),lTime,108)>=11 and convert(char(2),lTime,108)<=12
then '11:00-12:00'
when convert(char(2),lTime,108)>=12 and convert(char(2),lTime,108)<=13
then '12:00-13:00'
when convert(char(2),lTime,108)>=13 and convert(char(2),lTime,108)<=14
then '氏清13:00-14:00'
when convert(char(2),lTime,108)>=14 and convert(char(2),lTime,108)<=15
then '14:00-15:00'
when convert(char(2),lTime,108)>=15 and convert(char(2),lTime,108)<=16
then '15:00-16:00'
when convert(char(2),lTime,108)>=16 and convert(char(2),lTime,108)<=17
then '16:00-17:00'
when convert(char(2),lTime,108)>=17 and convert(char(2),lTime,108)<=18
then '17:00-18:00'
when convert(char(2),lTime,108)>=18 and convert(char(2),lTime,108)<=19
then '18:00-19:00'
when convert(char(2),lTime,108)>=19 and convert(char(2),lTime,108)<=20
then '19:00-20:00'
when convert(char(2),lTime,108)>=20 and convert(char(2),lTime,108)<=21
then '20:00-21:00'
when convert(char(2),lTime,108)>=21 and convert(char(2),lTime,108)<=22
then '21:00-22:00'
when convert(char(2),lTime,108)>=22 and convert(char(2),lTime,108)<=23
then '22:00-23:00'
when convert(char(2),lTime,108)>=23 and convert(char(2),lTime,108)<=24
then '23:00-00:00'
end as 時間段,sum(lNum)合計
from listtable group by
case when convert(char(2),lTime,108)>=0 and convert(char(2),lTime,108)<=1
then '00:00-01:00'
when convert(char(2),lTime,108)>=1 and convert(char(2),lTime,108)<=2
then '01:00-02:00'
when convert(char(2),lTime,108)>=2 and convert(char(2),lTime,108)<=3
then '02:00-03:00'
when convert(char(2),lTime,108)>=3 and convert(char(2),lTime,108)<=4
then '03:00-04:00'
when convert(char(2),lTime,108)>=4 and convert(char(2),lTime,108)<=5
then '04:00-05:00'
when convert(char(2),lTime,108)>=5 and convert(char(2),lTime,108)<=6
then '05:00-06:00'
when convert(char(2),lTime,108)>=6 and convert(char(2),lTime,108)<=7
then '06:00-07:00'
when convert(char(2),lTime,108)>=7 and convert(char(2),lTime,108)<=8
then '07:00-08:00'
when convert(char(2),lTime,108)>=8 and convert(char(2),lTime,108)<=9
then '08:00-09:00'
when convert(char(2),lTime,108)>=9 and convert(char(2),lTime,108)<=10
then '09:00-10:00'
when convert(char(2),lTime,108)>=10 and convert(char(2),lTime,108)<=11
then '10:00-11:00'
when convert(char(2),lTime,108)>=11 and convert(char(2),lTime,108)<=12
then '11:00-12:00'
when convert(char(2),lTime,108)>=12 and convert(char(2),lTime,108)<=13
then '12:00-13:00'
when convert(char(2),lTime,108)>=13 and convert(char(2),lTime,108)<=14
then '13:00-14:00'
when convert(char(2),lTime,108)>=14 and convert(char(2),lTime,108)<=15
then '14:00-15:00'
when convert(char(2),lTime,108)>=15 and convert(char(2),lTime,108)<=16
then '15:00-16:00'
when convert(char(2),lTime,108)>=16 and convert(char(2),lTime,108)<=17
then '16:00-17:00'
when convert(char(2),lTime,108)>=17 and convert(char(2),lTime,108)<=18
then '17:00-18:00'
when convert(char(2),lTime,108)>=18 and convert(char(2),lTime,108)<=19
then '18:00-19:00'
when convert(char(2),lTime,108)>=19 and convert(char(2),lTime,108)<=20
then '19:00-20:00'
when convert(char(2),lTime,108)>=20 and convert(char(2),lTime,108)<=21
then '20:00-21:00'
when convert(char(2),lTime,108)>=21 and convert(char(2),lTime,108)<=22
then '21:00-22:00'
when convert(char(2),lTime,108)>=22 and convert(char(2),lTime,108)<=23
then '22:00-23:00'
when convert(char(2),lTime,108)>=23 and convert(char(2),lTime,108)<=24
then '23:00-00:00' end
--表是ListTable 時間欄位lTime,數量欄位lNum
⑥ 在資料庫中查詢一段時間內的數據,SQL語句怎麼寫
例:查詢table表中,時間(time)在2016年5月1日之後的數據:
select * from table where to_char(time,'yyyy-mm-dd')>'2016-05-01';
註:to_char()函數適用於orcle資料庫,如果你用的是mysql資料庫,把to_char()替換成date_format()即可。
⑦ SQL語句查詢時間段的數據命令語句怎麼樣寫
代碼如下:
Select *
FROM j_GradeShop
Where (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000')
AND CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000') + 1)
orDER BY GAddTime DESC
小貼士:
--查詢當天:
select * from info where DateDiff(dd,datetime,getdate())=0
--查詢24小時內的:
select * from info where DateDiff(hh,datetime,getDate())<=24
--info為表名,datetime為資料庫中的欄位值 。
--查詢當天:
select * from info where DateDiff(dd,datetime,getdate())=0
--查詢24小時內的:
select * from info where DateDiff(hh,datetime,getDate())<=24
--info為表名,datetime為資料庫中的欄位值。
⑧ SQL語句查詢特定時間段的數據怎麼寫
SQL伺服器:
Select*fromtablewhere'2008-7-1'和'2008-12-31'
訪問:
從表中選擇發生日期>#2008-7-1#和發生日期<#2008-12-31#
就是這樣:注意,SQLserver中的日期和訪問有一點不同。
(8)sql時間段語句擴展閱讀:
SQL查詢日期語句
Select*fromShopOrderwheredatediff(week,ordTime,getdate()-1)=0//查詢第一年的日期
Select*fromShopOrder,其中datediff(day,ordTime,getdate()-1)=0//查詢當天的所有數據
SELECT * FROM A where datediff(d,datetime,getdate()) <=30 //前30天
SELECT * FROM A WHERE DATEDIFF(m, shijian, GETDATE()) <=1 //上個月
搜索當天記錄的其他方法:
SELECT*
FROMj_GradeShop
其中(GAddTimeBETWEENCONVERT(datetime,LEFT(GETDATE(),10)+'00:00:00.000'))
並轉換(datetime,LEFT(GETDATE(),10)+'00:00:00.00.000')+1)
由GAddTime指定的訂單
⑨ mysql查詢當天時間段的sql語句怎麼寫呢
你的問題描述的不夠明確,不太清楚你要的效果,這樣,給你兩種效果:
1、顯示某個時間段的數據:
select * from tb where hour(col_datetime)=18 and to_days(col_datetime)=to_days(curdate())
2、按時間段排序,顯示當天所有時間段數據:
select '今天'+cast(hour(col_datetime) as varchar(2))+'時發布的內容',* from tb where to_days(col_datetime)=to_days(curdate()) order by col_datetime
⑩ 如何在SQL中按時間段查詢數據
sql server:
select * from 表 where 發生日期>'2008-7-1' and 發生日期<'2008-12-31'
access:
select * from 表 where 發生日期>#2008-7-1# and 發生日期<#2008-12-31#
這樣就可以了,注意sql server與access中的日期有一點不一樣。
(10)sql時間段語句擴展閱讀:
sql查詢日期語句
select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查詢當天日期在一周年的數據
select * from ShopOrder where datediff(day,ordTime,getdate()-1)=0 //查詢當天的所有數據
SELECT * FROM A where datediff(d,datetime,getdate()) <=30 //前30天
SELECT * FROM A WHERE DATEDIFF(m, shijian, GETDATE()) <=1 //上一月
查詢當天記錄另類的方法:
SELECT *
FROM j_GradeShop
WHERE (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000')
AND CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000') + 1)
ORDER BY GAddTime DESC