简体   繁体   中英

Selecting Data Between Two Dates For Manually Inserted Dates

            SELECT * FROM users 
            WHERE birthday BETWEEN '08-06' AND  '15-06'

This is my query to select data between two dates. I know this is duplicate question but their is slight problem to my situation.

The Dates which I m storing are manually inserted to db using dropdown list which is varchar datatype

Problem: The Results for this query only selects data using date(dd) values and not compares whole date string

This is the result im getting which selects data from month 7 as well

    bday
   09-06-1985
   08-06-1990
   09-07-1991

Like other people write in comments, it's always best to use datetime data type for date. You can compare your varchar date like this

SELECT * FROM users 
WHERE STR_TO_DATE(birthday, '%d-%m')
  BETWEEN STR_TO_DATE('08-06', '%d-%m')
    AND STR_TO_DATE('15-06', '%d-%m')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM