如何将当前日期与MySQL中的日期范围或离散日期值进行比较?

时间:2017-03-07 04:47:36

标签: mysql sql csv date

我将数据库中的多个日期保存在一列中,用逗号(,)分隔,如

  1. 2017-03-05,2017-03-06,2017-03-07,2017-03-08,2017-03-08。
  2. 2017-03-05,2017-03-08
  3. 我只是想知道如何比较这些日期的当前日期。在第一个例子中,它很容易,但问题是第二个。

    任何人都可以帮助我吗?

    ...谢谢

5 个答案:

答案 0 :(得分:4)

正如其他人所评论的那样,最好将数据规范化并将各个日期存储在链接表的多行中,而不是存储在单个字段中。但假设有一个有效的理由不能做到这一点,以下将做你所问的:

SELECT date_csv,
       CASE WHEN CHAR_LENGTH(REPLACE(date_csv, ',', '')) = CHAR_LENGTH(date_csv) - 1
            THEN /* date_csv has a single comma */
                 CURRENT_DATE >= SUBSTRING(date_csv, 1, INSTR(date_csv, ',') - 1)
                 AND CURRENT_DATE <= SUBSTRING(date_csv, INSTR(date_csv, ',') + 1)
            ELSE /* date_csv has either no comma or multiple commas */
                 FIND_IN_SET(DATE_FORMAT(CURRENT_DATE,'%Y-%m-%d'), date_csv) > 0
       END AS 'Test result'

Rextester演示: http://rextester.com/EPKO65812

答案 1 :(得分:2)

  

您可以使用FIND_IN_SETCURRENT_DATE与逗号分隔进行比较   日期

SELECT FIND_IN_SET(CURRENT_DATE,'2017-03-05,2017-03-06,2017-03-07,2017-03-08,2017-03-08');  

修改

MySQL FIND_IN_SET()返回一个字符串的位置,如果它在一个字符串列表中存在(作为一个子字符串),那么对于你的第二个集合,它就会返回零

所以你可以做下面的事情

 SELECT * from tbl where FIND_IN_SET(CURRENT_DATE,'2017-03-05,2017-03-08')>0

答案 2 :(得分:2)

使用FIND_IN_SET

Thread 0 Crashed:
0   libsystem_kernel.dylib              0x000000018981f014 __pthread_kill + 8
1   libsystem_c.dylib                   0x0000000189793400 abort + 140
2   [AppName]                           0x00000001002d0c58 AppDelegate.(persistentStoreCoordinator.getter).(closure #1) (AppDelegate.swift:318)
3   [AppName]                           0x00000001002c7dec AppDelegate.persistentStoreCoordinator.getter (AppDelegate.swift:334)
4   [AppName]                           0x00000001002d0cbc AppDelegate.(managedObjectContext.getter).(closure #1) (AppDelegate.swift:338)
5   [AppName]                           0x00000001002c8054 AppDelegate.managedObjectContext.getter (AppDelegate.swift:342)
6   [AppName]                           0x00000001002c8198 AppDelegate.saveContext() (AppDelegate.swift:347)
7   [AppName]                           0x00000001002c7780 AppDelegate.applicationWillTerminate() (AppDelegate.swift:295)
8   [AppName]                           0x00000001002c77d8 @objc AppDelegate.applicationWillTerminate() (AppDelegate.swift:0)
9   UIKit                               0x0000000190792704 <redacted> + 244
10  UIKit                               0x00000001909947cc <redacted> + 792
11  UIKit                               0x0000000190997fdc <redacted> + 292
12  UIKit                               0x0000000190989d50 <redacted> + 560
13  UIKit                               0x00000001906f90b4 <redacted> + 168
14  CoreFoundation                      0x000000018a7fe0c0 <redacted> + 32
15  CoreFoundation                      0x000000018a7fbcf0 <redacted> + 372
16  CoreFoundation                      0x000000018a7fc180 <redacted> + 1024
17  CoreFoundation                      0x000000018a72a2b8 CFRunLoopRunSpecific + 444
18  GraphicsServices                    0x000000018c1de198 GSEventRunModal + 180
19  UIKit                               0x00000001907717fc <redacted> + 684
20  UIKit                               0x000000019076c534 UIApplicationMain + 208
21  [AppName]                           0x00000001002d1a94 main (AppDelegate.swift:17)
22  ???                                 0x000000018970d5b8 0x0 + 0

答案 3 :(得分:0)

如果我有这样糟糕的数据结构,我肯定会使用find_in_set()substring_index()

select t.*
from t
where find_in_set(date_format(curdate(), '%Y-%m-%d'), datecol) > 0 or
      (datecol like '%,%' and datecol not like '%,%,%' and
       date_format(curdate(), '%Y-%m-%d') between substring_index(datecol, ',', 1) and substring_index(datecol, ',', -1)
      );

答案 4 :(得分:-1)

<?php

$string_date='2017-03-05,2017-03-06,2017-03-07,2017-03-08,2017-03-08';
$tmp_dates=explode(',',$string_date);

foreach($tmp_dates as $tmp_date)
{
$newDateString = date_format(date_create_from_format('Y-m-d', $tmp_date), 'Y-m-d');

$interval =date_diff(date_create($newDateString),date_create(date('Y-m-d')));
echo $interval->format('%R%a days');
echo '<br>';
}
?>