显示“不久前的日期”的 SQL 查询例如“一周前”、“两周前”、“一个月前”、“一年前”等
我需要一个以以下格式显示日期的查询:
过去 7 天内的日期 -> “一周前” 过去 7 至 14 天内的日期 -> “两周前” 等等...
过去 30 天内的日期 -> “一个月前” 过去 30 至 60 天内的日期 -> “两个月前 等等..
过去365天内的日期-> “一年前” 过去 365 至 730 天内的日期 -> “两年前 等等...
如果你们能指出我正确的方向,我将不胜感激。
谢谢
I need a query that displays dates in the following format:
Dates that fall in the past 7 days -> “one week ago”
Dates that fall in the past 7 to 14 days -> “two week ago”
Etc…
Dates that fall in the past 30 days -> “one month ago”
Dates that follow in the past 30 to 60 days -> “two months ago
Etc..
Dates that fall in the past 365 days -> “one year ago”
Dates that fall in the past 365 to 730 days -> “two years ago
Etc...
If you guys can point me to the right direction I’ll appreciate it.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我写的一个名为 time_ago 的 mysql 函数,
它是这样使用的
,结果如下所示:
编辑:
我修改了这个答案以提供该函数的改进版本:新版本使用 DECLARE 而不是设置会话变量。
Here is a mysql function I wrote called time_ago
It is used like this
And the result looks like this:
EDIT:
I have modified this answer to offer an improved version of this function: the new version uses DECLARE instead of setting session variables.
如上所述,在 SQL 查询中使用 case 语句。像这样的东西:
有关 MS SQL 的更多信息: http://msdn.microsoft.com /en-us/library/ms181765.aspx
(或者参阅您的 SQL Server 品牌的文档)
As stated above, use a case statement in your SQL query. Something like this:
More Information for MS SQL: http://msdn.microsoft.com/en-us/library/ms181765.aspx
(Or see the documentation for your SQL server brand)