0

I have a table which is 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 ...

Search Query Using >= 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

   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?

sanjay joon
  • 133
  • 1
  • 14

3 Answers3

2
stime      | etime 
------------------------ 
15-04-2016 | 31-06-2016

That format means your fields are varchars and not date hence you can not compare them ordinarily like dates can be compared.

If you were using proper date type field, format would have been YYYY-MM-DD and then any standard comparison would work just fine. 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

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

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

EDIT

sqlfiddle example

Scooter Daraf
  • 525
  • 7
  • 23
0

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.

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.

You can achieve this in two way,
Option 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.

Option 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
Piyush Gupta
  • 2,181
  • 3
  • 13
  • 28