[英]query between two dates using SQL?
I have a table which is clientbike 我有一张桌子是clientbike
SELECT stime,etime FROM clientbike
type = datetime
Result 结果
stime | etime
--------------------------------------------
2016-04-18 00:00:00 | 2016-05-18 00:00:00
--------------------------------------------
2016-04-05 00:00:00 | 2016-06-05 00:00:00
--------------------------------------------
2016-04-03 00:00:00 | 2016-07-03 00:00:00
Now I want to search function: 现在我要搜索功能:
all query result found 0 but i want 3 ... 所有查询结果都找到0但我想要3 ...
Search Query Using >= AND <= 使用> = AND <=搜索查询
SELECT stime,etime FROM clientbike WHERE
stime >= '2016-04-25 00:00:00' AND
etime <= '2016-04-26 00:00:00'
Wrong 错误
No Result Found...
search query using between 搜索查询之间使用
SELECT stime,etime FROM clientbike WHERE
stime BETWEEN '2016-04-25 00:00:00' AND '2016-04-26 00:00:00' AND
etime BETWEEN '2016-04-25 00:00:00' AND '2016-04-26 00:00:00'
Wrong 错误
No Result Found...
search query using between with str_to_date 在str_to_date之间使用搜索查询
SELECT stime,etime FROM clientbike WHERE
STR_TO_DATE(stime, '%Y-%m-%d') BETWEEN '2016-04-25 00:00:00' AND '2016-04-26 00:00:00' AND
STR_TO_DATE(etime, '%Y-%m-%d') BETWEEN '2016-04-25 00:00:00' AND '2016-04-26 00:00:00'
Wrong 错误
No Result Found...
Can someone explain this behavior? 有人可以解释这种行为吗?
stime | etime
------------------------
15-04-2016 | 31-06-2016
That format means your fields are varchar
s and not date
hence you can not compare them ordinarily like date
s can be compared. 这种格式意味着您的字段是
varchar
而不是date
因此通常无法像date
一样进行比较。
If you were using proper date
type field, format would have been YYYY-MM-DD
and then any standard comparison would work just fine. 如果您使用正确的
date
类型字段,则格式应YYYY-MM-DD
,然后进行任何标准比较都可以。 Since that is not the case you have to convert these strings to dates to compare them correctly. 由于不是这种情况,您必须将这些字符串转换为日期以正确比较它们。
Best course of action is to change your fields to use correct data type for this purpose, and that is DATE
最佳做法是将字段更改为为此目的使用正确的数据类型,即
DATE
you have to convert your columns to be as dates 您必须将列转换为日期
STR_TO_DATE(stime, '%Y-%m-%d')
Please try this 请尝试这个
SELECT stime,etime FROM clientbike
WHERE STR_TO_DATE(stime, '%Y-%m-%d') BETWEEN '2016-04-01' AND '2016-04-17'
AND STR_TO_DATE(etime, '%Y-%m-%d') BETWEEN '2016-05-01' AND '2016-07-26'
or 要么
in your database change that column from varchar to date or datetime 在数据库中将该列从varchar更改为date或datetime
EDIT 编辑
First thing I can see in your question your stime and etime column is string type so change data type to date in database and for debugging you can check below query. 我首先看到的问题是您的stime和etime列是字符串类型,因此将数据类型更改为数据库中的日期,并进行调试,可以检查以下查询。
You need to just copy below query and execute in mysql, then you can see this query is returning number of count entry from staring date to end date. 您只需要复制下面的查询并在mysql中执行,就可以看到此查询正在返回从凝视日期到结束日期的计数条目数。
You can achieve this in two way, 您可以通过两种方式实现这一目标,
Option 1 选项1
SET @FromDate := '2016-04-1'
SET @ToDate := '2016-04-30'
SELECT COUNT(* ) FROM clientbike
WHERE stime >= @FromDate AND etime <= @ToDate
This is covering all date range scenario like selected @FromDate
and @ToDate
both are within start and end column in table. 这涵盖了所有日期范围的情况,例如所选的
@FromDate
和@ToDate
都在表的开始和结束列中。
Option 2 选项2
SET @FromDate := '2016-04-1'
SET @ToDate := '2016-04-30'
SELECT count(*) FROM clientbike WHERE stime BETWEEN @FromDate AND @ToDate AND etime BETWEEN @FromDate AND @ToDate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.