作者:美食和旅丶行_379 | 来源:互联网 | 2023-10-14 22:43
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 个解决方案