MySQL query returns wrong result count between a date range
NickName:Owais Ask DateTime:2015-06-11T12:48:11

MySQL query returns wrong result count between a date range

I have created a query which returns results between a date range, however the previous developer has shoot me on the foot by saving the date as a varchar(10) in MySQL and also in two formats (Y-m-d and d-m-Y).

My problem is that when I select results between a date from 01-June-2015 to 11-June-2015, it gave me 18 results for June 2015.

But when I select results between a date from 01-june-2014 to 11-June-2015, it gave me 13 results for June 2015 which must be 18 results, I think.

TASK: GET TOTAL OF NEW VISITORS IN A DATE RANGE BUT DATE IS NOT PRESENT IN VISITOR TABLE SO I TAKE HIS VERY FIRST VISIT DATE FROM VISIT TABLE That's why I use MIN() function

MySQL query is:

SELECT min(visits.id), t.id, t.first_name,
    visits.date_check_in
FROM `visitor` `t`
    JOIN `visit` `visits` ON visits.visitor = t.id
WHERE 
  ((STR_TO_DATE(visits.date_check_in, '%d-%m-%Y') BETWEEN
    STR_TO_DATE('11-06-2014', '%d-%m-%Y') AND STR_TO_DATE('11-06-2015',
    '%d-%m-%Y')) 
OR (STR_TO_DATE(visits.date_check_in, '%Y-%m-%d') BETWEEN
    STR_TO_DATE('2014-06-11', '%Y-%m-%d') AND STR_TO_DATE('2015-06-11',
    '%Y-%m-%d')) ) 
AND (t.is_deleted = 0) 
AND (visits.is_deleted = 0)
AND (visits.visit_status != 2)
GROUP BY `t`.`id`

kindly help me what am I doing wrong. I have spent about 4 days by thinking and doing different things but got stuck. I hope to hear from you quickly, thanks.

Copyright Notice:Content Author:「Owais」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30771780/mysql-query-returns-wrong-result-count-between-a-date-range

More about “MySQL query returns wrong result count between a date range” related questions

MySQL query returns wrong result count between a date range

I have created a query which returns results between a date range, however the previous developer has shoot me on the foot by saving the date as a varchar(10) in MySQL and also in two formats (Y-m-...

Show Detail

mysql DATE range BETWEEN Query

I have some very odd behaviour from a MySQL query in my C# application which is pulling rows from a Table called project_info. It should retrieve all the Rows which Equal the Customer's Name, and the

Show Detail

Single MySQL query summing results between a date and having count

I am looking for a single query that would give me the total sum of occurrences of a client_id who appear only once between a certain date range. I have the query below which returns to the total ...

Show Detail

MYSQL- Having with between returns empty result

Following is the query i am trying to run. SELECT p_date,TS.substationid,ts.substationcode,ts.manufacturingproductid,ts.assemblylineid,ts.teamid,ts.subofficecode, TA.AllowID,ta.allowname,ta.minben...

Show Detail

Count of records between min date range and other date

I'm trying to get the count of records of users who appear between a certain date range, specifically the min(date) for each unique user and that min(date) + 14 days. I've checked this link SQL HAV...

Show Detail

Active Record query to find parents with child within a date range and child prior to that date range

I need an active record query that returns the count of Parents with a Child created within a date range AND a Child created prior to the date range. I currently have the following two queries: This

Show Detail

Evaluate mysql date range

I have a mysql table with some date ranges, for example: start: 2015-09-10 end: 2015-09-10 Now, I have to check if another date range is inside the start-end date, even if only for some days. 20...

Show Detail

MySql Query For Date Range in PHP

I have a MySql database that uses a timestamp field to track when the entry was inserted. Now I want to query the table for entries by a date range. I would prefer to use MySql to extract the rec...

Show Detail

mysql query counts nothing

Hey guys I have a mysql table holding a few records as shown in the picture bellow! I have the following query to count the number of records (number of records for sr13, r4 and huawei) in the spe...

Show Detail

How to count at specific date range in mysql

I have created this mysql code to count at only specific date range, i dumped the output of my mysql code and here it is: SELECT COUNT( IF( DATE(q.date_emai...

Show Detail