简体   繁体   English

使用MySQL查找日期是否在日期范围内

[英]Find if a date is in date range using MySQL

Hi I have following values stored in MySQL table: 嗨,我在MySQL表中存储了以下值:

--- ------------------------     -------------------
Id | StartDate (VARCHAR (20))  | EndDate(VARCHAR(20))
--- ------------------------     -------------------
1  | 03-04-2017                | 18-04-2017

I am using the following SQL to find if the date is within the StartDate and EndDate: 我正在使用以下SQL查找日期是否在StartDate和EndDate内:

SELECT 
(date_format(str_to_date('03-04-2017','%d-%m-%Y'),'%d-%m-%Y') >= StartDate 
AND 
date_format(str_to_date('03-04-2017','%d-%m-%Y'),'%d-%m-%Y') <= EndDate) AS 
valid 
FROM holiday

My issue is it that when I execute the query and provide 03-04-2017 it returns 1 but also returns 1 when I provide 03-05-2017. 我的问题是,当我执行查询并提供03-04-2017时,它返回1,但是当我提供03-05-2017时也返回1。

Could someone please highlight what is wrong in this query? 有人可以强调一下此查询中的错误吗?

Use a query like this: 使用如下查询:

SELECT *
FROM holiday
WHERE
    STR_TO_DATE(StartDate,'%d-%m-%Y')
BETWEEN 
    str_to_date('03-04-2017','%d-%m-%Y')
AND
    str_to_date('03-04-2017','%d-%m-%Y');

sample 样品

mysql> SELECT IF(STR_TO_DATE('11-04-2017','%d-%m-%Y')
    ->             BETWEEN
    ->                 str_to_date('03-04-2017','%d-%m-%Y')
    ->             AND
    ->                 str_to_date('10-04-2017','%d-%m-%Y')
    ->
    -> ,'YES','NO') AS ANSWER;
+--------+
| ANSWER |
+--------+
| NO     |
+--------+
1 row in set (0,00 sec)

mysql> SELECT IF(STR_TO_DATE('04-04-2017','%d-%m-%Y')
    ->             BETWEEN
    ->                 str_to_date('03-04-2017','%d-%m-%Y')
    ->             AND
    ->                 str_to_date('10-04-2017','%d-%m-%Y')
    ->
    -> ,'YES','NO') AS ANSWER;
+--------+
| ANSWER |
+--------+
| YES    |
+--------+
1 row in set (0,01 sec)

mysql>

You can use BETWEEN operator to compare the dates, eg: 您可以使用BETWEEN运算符比较日期,例如:

SELECT * 
FROM `table` 
WHERE '2017-04-03' BETWEEN start_date AND end_date;

Update 更新

If the dates are stored as varchar then you need to convert it to date before comparing, eg: 如果日期存储为varchar则需要在比较之前将其转换为date ,例如:

SELECT * 
FROM table 
WHERE '2017-04-03' BETWEEN STR_TO_DATE(start_date, '%d-%m-%Y') AND STR_TO_DATE(end_date, '%d-%m-%Y');

Here's the SQL Fiddle . 这是SQL Fiddle

All other answers are good for your question but in my opinion you should convert your database. 所有其他答案都可以解决您的问题,但我认为您应该转换数据库。

It's only sane option. 这只是理智的选择。

Using dates in weird VARCHAR format will have big impact in future. 使用奇怪的VARCHAR格式的日期将对将来产生重大影响。 Not only it impacts perfomances of your tables right now but you are missing whole MySQL date API ecosystem because of it. 它不仅会立即影响您的表性能,而且由于它会丢失整个MySQL日期API生态系统。

  1. Add temporary columns let's say tmp_start_time DATE 添加临时列,比如说tmp_start_time DATE
  2. Fill them with dates UPDATE holiday SET tmp_start_time = str_to_date(start_time,'%d-%m-%Y') 用日期填充UPDATE holiday SET tmp_start_time = str_to_date(start_time,'%d-%m-%Y')
  3. Drop old varchar keys in table 在表中删除旧的varchar键
  4. Add same keys but as DATE 添加相同的键,但在DATE
  5. Update them UPDATE holiday SET start_time = tmp_start_time 更新它们UPDATE holiday SET start_time = tmp_start_time

From now on you would be able to use BETWEEN as everyone else without str_to_date 从现在开始,您无需str_to_date就可以像其他所有人一样使用BETWEEN


I just found your comment 我刚刚找到你的评论

unfortunately I cannot change schema 不幸的是我无法更改架构

ask yourself twice: are you sure? 问问自己两次:确定吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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