5

我试图只抓取特定日期范围内的记录。问题是时间戳和日期作为字符串存储在同一个单元格中。我只想抓取日期介于 2013-05-01 和 2013-05-03 之间的行。

日期(存储为字符串)

2013-05-01T23:19:44
2013-05-02T23:19:40
2013-05-06T23:19:46
2013-05-06T23:15:17

mysql

SELECT * FROM table WHERE date BETWEEN 2013-05-01 AND 2013-05-03
4

4 回答 4

10

尝试

SELECT *
  FROM table1
WHERE STR_TO_DATE(`date`,'%Y-%m-%d') BETWEEN '2013-05-01' AND '2013-05-03'

SQLFiddle

正如@FreshPrinceOfSO 绝对正确指出的那样,在这种情况下不会使用任何索引

于 2013-05-07T17:25:28.287 回答
1
SELECT * FROM table1 
WHERE STR_TO_DATE(SUBSTRING(`date`,1,10),'%d-%m-%Y') 
    BETWEEN '2013-05-01' AND '2013-05-03'
于 2013-05-07T17:22:53.253 回答
0

字符串对于 . 来说几乎是有效的语法datetime。因此,另一种可能更慢的方法是将 'T' 替换为空格,然后将其转换为datetime.

SELECT * FROM table1 WHERE
  CAST(REPLACE(`date`, 'T', ' ') AS DATETIME) 
  BETWEEN '2013-05-01' AND '2013-05-03';
于 2013-05-07T17:46:49.453 回答
-1
SELECT * FROM table 
WHERE date('yourdate') BETWEEN date('2013-05-01') AND date('2013-05-03')
于 2015-12-14T17:11:30.210 回答