如何在SQL中按时间段查询数据

发布网友

我来回答

11个回答

懂视网

24小时内上线商品:
SELECT * FROM goods
WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= add_time;
今天上线商品:
select * from goods where to_days(add_time)=to_days(now());
======参考=========
mysql查询今天、昨天、7天、近30天、本月数据

  今天

  select * from 表名 where to_days(时间字段名)=to_days(now());

  昨天

  SELECT * FROM `表名` where TO_DAYS(NOW()) – TO_DAYS(时间字段名) = 1;

  /*DATEDIFF(now() , FROM_UNIXTIME(`时间字段名`)) = 1; //时间字段 存储类型为时间戳*/

  7天

  SELECT * FROM `表名` where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名);

  /*DATEDIFF(now() , FROM_UNIXTIME(`时间字段名`)) = 7; //时间字段 存储类型为时间戳*/

  近30天

  SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名);

  本月

  SELECT * FROM 表名 WHERE DATE_FORMAT(时间字段名, ‘%Y%m‘ = DATE_FORMAT(CURDATE(), ‘%Y%m‘;

根据时间查询的sql格式

标签:sql   php   

热心网友

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中的日期有一点不一样。

扩展资料:

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

热心网友

select
*
from
ms_cf01
a
where
a.kfrq
between
to_date('20100101
180000','yyyymmdd
hh24miss')
and
to_date('20101231
180000','yyyymmdd
hh24miss')
and
to_char(a.kfrq,'hh24miss')
between
'180000'
and
'240000'
主要用到
to_char,to_date对时间字段的转换方法,具体使用方法可
如果这么查询,主要是第2个条件无法用上索引,所以最好的方式是在涉及表的时候将该字段拆成2个字段
日期
,时间,并用整形表示

热心网友

sql
server与access中的日期有一点不一样,
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:

select * from 表 where 发生日期>'2008-7-1' and 发生日期<'2008-12-31'

access:

select * from 表 where 发生日期>#2008-7-1# and 发生日期<#2008-12-31#

这样就可以了,注意sql server与access中的日期有一点不一样。

热心网友

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中的日期有一点不一样。
扩展资料:
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

热心网友

这样就可以了啊:
1.select *
from table_name where 发生日期 between '2008-07-01'and '2008-12-31'

2.和上面同理哈

祝你好运!

热心网友

sql server与access中的日期有一点不一样,
sql server:
select * from 表 where 发生日期>'2008-7-1' and 发生日期<'2008-12-31'
access:
select * from 表 where 发生日期>#2008-7-1# and 发生日期<#2008-12-31#
呵呵,希望能有帮助,^_^

热心网友

select * from 表where 发生日期 between cast('2008-07-01'as datetime )

and cast('2008-12-31'as datetime)

热心网友

可以用between……and

热心网友

select * from 表 where 发生日期>'2008-7-1' and 发生日期<'2008-12-31'

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com