当前位置:首页 » 编程语言 » sql时间段语句

sql时间段语句

发布时间: 2023-06-01 15:29:43

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

热点内容
内置存储卡可以拆吗 发布:2025-05-18 04:16:35 浏览:333
编译原理课时设置 发布:2025-05-18 04:13:28 浏览:376
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:610
java用什么软件写 发布:2025-05-18 03:56:19 浏览:31
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:107
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:941
安卓手机如何拍视频和苹果一样 发布:2025-05-18 03:40:47 浏览:739
为什么安卓手机连不上苹果7热点 发布:2025-05-18 03:40:13 浏览:802
网卡访问 发布:2025-05-18 03:35:04 浏览:510
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:371