mysql怎么根据时间戳查询指定日期内数据

mysql怎么根据时间戳查询指定日期内数据,第1张

select from tbl where timestamp between UNIX_TIMESTAMP('2011-06-01 00:00:00') and UNIX_TIMESTAMP('2011-07-01 00:00:00');

这样只能用以下函数拆分数组变成一个数字,然后在筛选
SUBSTRING_INDEX(str, delim, count)
str: 要处理的字符串
delim: 分割符
count: 计数 如果为正数,则从左开始数,如果为负数,则从右开始数
比如:你那个时间戳为STIME字段 然后在where 条件中SUBSTRING_INDEX(STIME, ',', 10)>'1531152000' and SUBSTRING_INDEX(STIME, ',', -10)<'1531152003'
这样试试可不可以满足你的需求

下面是时间戳查询。如果数据库时间显示的是 2011-04-05 那就不需要 用 strtotime 时间戳转换函数:

$timea = strtotime($_POST['timea']);

$timeb = strtotime($_POST['timeb']);

$sq2="select from `ecs_order_info` where add_time between '$timea' and '$timeb' and `quanxian`='$dangqian' order by `order_id` DESC limit 50";

$sql = mysql_query($sq2);

扩展资料

在php中完成

1、UNIX时间戳转换为日期用函数: date()

一般形式:date('Y-m-d H:i:s', 1156219870);

2、日期转换为UNIX时间戳用函数:strtotime()

一般形式:strtotime('2010-03-24 08:15:42');

在MySQL中完成

这种方式在MySQL查询语句中转换,优点是不占用PHP解析器的解析时间,速度快,缺点是只能用在数据库查询中,有局限性。

1、UNIX时间戳转换为日期用函数: FROM_UNIXTIME()

一般形式:select FROM_UNIXTIME(1156219870);

2、日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP()

一般形式:Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);

举例:mysql查询当天的记录数:

$sql=”select from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') order by id desc”。

使用where方法
where方法支持时间比较,例如:
//
大于某个时间
where('create_time','>
time','2016-1-1');
//
小于某个时间
where('create_time','<=
time','2016-1-1');
//
时间区间查询
where('create_time','between
time',['2015-1-1','2016-1-1']);
第三个参数可以传入任何有效的时间表达式,会自动识别你的时间字段类型,支持的时间类型包括timestamps、datetime、date和int。
使用whereTime方法
whereTime方法提供了日期和时间字段的快捷查询,示例如下:
//
大于某个时间
db('user')
->whereTime('birthday',
'>=',
'1970-10-1')
->select();
//
小于某个时间
db('user')
->whereTime('birthday',
'<',
'2000-10-1')
->select();
//
时间区间查询
db('user')
->whereTime('birthday',
'between',
['1970-10-1',
'2000-10-1'])
->select();
//
不在某个时间区间
db('user')
->whereTime('birthday',
'not
between',
['1970-10-1',
'2000-10-1'])
->select();
时间表达式
还提供了更方便的时间表达式查询,例如:
//
获取今天的博客
db('blog')
->whereTime('create_time',
'today')
->select();
//
获取昨天的博客
db('blog')
->whereTime('create_time',
'yesterday')
->select();
//
获取本周的博客
db('blog')
->whereTime('create_time',
'week')
->select();
//
获取上周的博客
db('blog')
->whereTime('create_time',
'last
week')
->select();
//
获取本月的博客
db('blog')
->whereTime('create_time',
'month')
->select();
//
获取上月的博客
db('blog')
->whereTime('create_time',
'last
month')
->select();
//
获取今年的博客
db('blog')
->whereTime('create_time',
'year')
->select();
//
获取去年的博客
db('blog')
->whereTime('create_time',
'last
year')
->select();
如果查询当天、本周、本月和今年的时间,还可以简化为:
//
获取今天的博客
db('blog')
->whereTime('create_time',
'd')
->select();
//
获取本周的博客
db('blog')
->whereTime('create_time',
'w')
->select();
//
获取本月的博客
db('blog')
->whereTime('create_time',
'm')
->select();
//
获取今年的博客
db('blog')
->whereTime('create_time',
'y')
->select();
V505+版本开始,还可以使用下面的方式进行时间查询
//
查询两个小时内的博客
db('blog')
->whereTime('create_time','-2
hours')
->select();
这些在开发手册中都可以找到的。希望可以帮到你。

SqlConnection MyConnection = new SqlConnection();
MyConnectionConnectionString = str;
MyConnectionOpen();
DataSet MyDataSet;
SqlCommand MyCommand = new SqlCommand();
MyCommandCommandType = CommandTypeText;
MyCommandConnection = MyConnection; 
string SQL = "select a,b from table1";
MyCommandCommandText = SQL; 
SqlDataAdapter MyAdapter = new SqlDataAdapter(); 
MyAdapterSelectCommand = MyCommand; 
MyAdapterFill(MyDataSet, "DataSet1"); 
MySetTables[0]Rows[0][列名或列序号];


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13378743.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-24
下一篇 2023-07-24

发表评论

登录后才能评论

评论列表(0条)

保存