例如,如果我有搜索字符串“2017-12-14”,那么我需要查找与7天范围相匹配的所有行:“% - 12-11”,“% - 12-12”,“% -12-13“,”% - 12-14“,”% - 12-15“,”% - 12-16“,”% - 12-17“。
是否可以仅使用MySql?
答案 0 :(得分:0)
是的,这是可能的。
您正在寻找的功能是+/- INTERVAL expr unit
。见MySQL Date and Time Functions
为了让7天后再使用- INTERVAL 7 DAY
:
SELECT *
FROM tablename
WHERE DATE(timefield) BETWEEN '2017-12-14' - INTERVAL 7 DAY AND '2017-12-14'
根据您的示例,只需使用-INTERVAL 1 WEEK
:
SELECT *
FROM tablename
WHERE DATE(timefield) BETWEEN '2017-12-14' - INTERVAL 1 WEEK AND '2017-12-14'
MICROSECOND第二个星期每一个小时的第一个星期 SECOND_MICROSECOND MINUTE_MICROSECOND MINUTE_SECOND HOUR_MICROSECOND HOUR_SECOND HOUR_MINUTE DAY_MICROSECOND DAY_SECOND DAY_MINUTE DAY_HOUR YEAR_MONTH
答案 1 :(得分:0)
要选择同一天的交叉年份,您可以使用以下技巧。
{r message=FALSE}
comparison_data <- spooky_trainining_tidy_1n %>%
dplyr::select(author, word) %>%
dplyr::anti_join(stop_words) %>%
dplyr::count(author,word, sort = TRUE)
comparison_data %>%
reshape2::acast(word ~ author, value.var = "n", fill = 0) %>%
comparison.cloud(colors = c("red", "violetred4", "rosybrown1"),
random.order = F,
scale=c(7,.5),
rot.per = .15,
max.words = 200)
NOW() - INTERVAL 1 WEEK
。
DAYOFYEAR
注意:闰年不计算!
根据我根据闰年进行的简短调查,扩展SQL查询的容忍度为1天会更容易,即使用SELECT * FROM timevalues
WHERE DAYOFYEAR(timefield) IN (
SELECT DAYOFYEAR(timefield)
FROM timevalues
WHERE DATE(timefield) BETWEEN NOW() - INTERVAL 1 WEEK AND NOW()
)
;
代替- INTERVAL 8 DAY
,然后控制当天的有效性。在循环中处理数据期间的数据库。