热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将日期分类为“昨天”,“上周”等-Classifyingdateas“yesterday”,“lastweek”,etc

Ihaveatablelikethis:我有这样一张桌子:mytable+----+------------+|id|date_time|+----+------

I have a table like this:

我有这样一张桌子:

// mytable
+----+------------+
| id | date_time  |
+----+------------+
| 1  | 1464136759 | -- 5 days ago
| 2  | 1464436759 | -- 2 days ago
| 3  | 1464538248 | -- 6 hours ago
+----+------------+
--                     ^ these are based on current time which is 1464561158

Also I have this query:

我也有这个问题:

SELECT id, CASE DATE(FROM_UNIXTIME(date_time))
           WHEN CURDATE() THEN 'today' 
           WHEN CURDATE() - INTERVAL 1 DAY THEN 'yesterday'
           WHEN CURDATE() - INTERVAL 7 DAY THEN 'in last week'
           ELSE 'in last month or more'
           END range
FROM mytable
WHERE 1

And here is current output:

这是当前的输出:

+----+---------------+
| id |     range     |
+----+---------------+
| 1  | in last month |
| 2  | in last month |
| 3  | yesterday     |
+----+---------------+

As you see my question selects all those unix-times wrong. Why and how can I fix it?

如你所见,我的问题选择了所有那些unix-times错误。为什么以及如何解决它?

Here is expected output:

这是预期的输出:

+----+--------------+
| id |    range     |
+----+--------------+
| 1  | in last week |
| 2  | yesterday    | 
| 3  | today        |
+----+--------------+

2 个解决方案

#1


3  

Presumably, the logic that you want is like this:

据推测,你想要的逻辑是这样的:

SELECT id,
       (CASE WHEN FROM_UNIXTIME(date_time) >= CURDATE() THEN 'today'
             WHEN FROM_UNIXTIME(date_time) >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN 'yesteray'
             WHEN FROM_UNIXTIME(date_time) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) THEN 'in last week'
             ELSE 'in last month or more'
        END) as `range`
FROM mytable
WHERE 1;

Notes:

笔记:

  • There is no reason to extract the date.
  • 没有理由提取日期。
  • Clauses in a case statement are executed in sequence, so the first matching one will return a value.
  • case语句中的子句按顺序执行,因此第一个匹配的子句将返回一个值。
  • If your original code is failing, then this might fail as well. Your original code should not have been returning only "yesterday".
  • 如果您的原始代码失败,那么这也可能会失败。您的原始代码不应该只返回“昨天”。
  • range is a reserved word, so it needs to be escaped.
  • range是保留字,因此需要进行转义。

Here is the SQL Fiddle.

这是SQL小提琴。

#2


2  

The issue is you arent checking a range for the week on your third condition. Meaning...

问题是你没有在第三个条件下检查一周的范围。含义...

DATE(FROM_UNIXTIME(date_time)) BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() - INTERVAL 1 DAY

FIDDLE

小提琴

Currently your code is saying WHEN CURDATE() - INTERVAL 7 DAY THEN 'in last week' if the date is equal to 7 days ago ONLY. the 25th is not the 22nd so it fails that check. You need to specify a range in order to get it to work.

目前你的代码正在说什么CURDATE() - 如果日期等于7天前,那么在上周的“间隔7天那天”。 25日不是第22次所以它没有通过检查。您需要指定一个范围才能使其正常工作。


推荐阅读
author-avatar
美食和旅丶行_379
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有