显示“不久前的日期”的 SQL 查询例如“一周前”、“两周前”、“一个月前”、“一年前”等

发布于 2024-12-03 08:49:01 字数 264 浏览 5 评论 0原文

我需要一个以以下格式显示日期的查询:

过去 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋千易 2024-12-10 08:49:01

这是我写的一个名为 time_ago 的 mysql 函数,

DELIMITER $
DROP FUNCTION IF EXISTS time_ago;
CREATE FUNCTION time_ago (ts datetime) 
RETURNS varchar(255)
DETERMINISTIC
BEGIN 
    DECLARE utx INT SIGNED DEFAULT 1;
    DECLARE nowutx INT SIGNED DEFAULT 1;
    DECLARE dif INT SIGNED DEFAULT 1;
    DECLARE method varchar(255);
    DECLARE cnt varchar(255);
    DECLARE plural tinyint(11);
    DECLARE future tinyint(11);
    SET utx := UNIX_TIMESTAMP(ts);
    SET nowutx := UNIX_TIMESTAMP(NOW());
    SET future := utx > nowutx;
    SET dif := IF(future, utx - nowutx, nowutx - utx);
        SET method := IF(dif < 60, 'Second', IF(
                                    dif < (60 * 60), 'Minute', IF(
                                        dif < (60 * 60 * 24), 'Hour', IF(
                                            dif < (60 * 60 * 24 * 7), 'Day' , IF(
                                                dif < (60 * 60 * 24 * 365), 'Week', 'Year')))));

        SET cnt := IF(dif < 60, dif, IF(
                                    dif < (60 * 60), floor(dif / 60), IF(
                                        dif < (60 * 60 * 24), floor(dif / (60 * 60)), IF(
                                            dif < (60 * 60 * 24 * 7), floor(dif / (60 * 60 * 24)) , IF(
                                                dif < (60 * 60 * 24 * 365) , floor(dif / (60 * 60 * 24 * 7)), floor(dif / (60 * 60 * 24 * 365)))))));

        SET plural := cnt != 1;

        return CONCAT(IF(future, 'In ', ''), cnt, ' ',method, IF(plural, 's', '') , IF(future, ' From Now', ' Ago'));
END$
DELIMITER ;

它是这样使用的

SELECT time_ago(date_ordered) time_ago FROM orders LIMIT 1

,结果如下所示:

time_ago
22 Weeks Ago

编辑:

我修改了这个答案以提供该函数的改进版本:新版本使用 DECLARE 而不是设置会话变量。

Here is a mysql function I wrote called time_ago

DELIMITER $
DROP FUNCTION IF EXISTS time_ago;
CREATE FUNCTION time_ago (ts datetime) 
RETURNS varchar(255)
DETERMINISTIC
BEGIN 
    DECLARE utx INT SIGNED DEFAULT 1;
    DECLARE nowutx INT SIGNED DEFAULT 1;
    DECLARE dif INT SIGNED DEFAULT 1;
    DECLARE method varchar(255);
    DECLARE cnt varchar(255);
    DECLARE plural tinyint(11);
    DECLARE future tinyint(11);
    SET utx := UNIX_TIMESTAMP(ts);
    SET nowutx := UNIX_TIMESTAMP(NOW());
    SET future := utx > nowutx;
    SET dif := IF(future, utx - nowutx, nowutx - utx);
        SET method := IF(dif < 60, 'Second', IF(
                                    dif < (60 * 60), 'Minute', IF(
                                        dif < (60 * 60 * 24), 'Hour', IF(
                                            dif < (60 * 60 * 24 * 7), 'Day' , IF(
                                                dif < (60 * 60 * 24 * 365), 'Week', 'Year')))));

        SET cnt := IF(dif < 60, dif, IF(
                                    dif < (60 * 60), floor(dif / 60), IF(
                                        dif < (60 * 60 * 24), floor(dif / (60 * 60)), IF(
                                            dif < (60 * 60 * 24 * 7), floor(dif / (60 * 60 * 24)) , IF(
                                                dif < (60 * 60 * 24 * 365) , floor(dif / (60 * 60 * 24 * 7)), floor(dif / (60 * 60 * 24 * 365)))))));

        SET plural := cnt != 1;

        return CONCAT(IF(future, 'In ', ''), cnt, ' ',method, IF(plural, 's', '') , IF(future, ' From Now', ' Ago'));
END$
DELIMITER ;

It is used like this

SELECT time_ago(date_ordered) time_ago FROM orders LIMIT 1

And the result looks like this:

time_ago
22 Weeks Ago

EDIT:

I have modified this answer to offer an improved version of this function: the new version uses DECLARE instead of setting session variables.

撩人痒 2024-12-10 08:49:01

如上所述,在 SQL 查询中使用 case 语句。像这样的东西:

SELECT 
Column1, 
Column2, 
theDate,
CASE
  WHEN DATEDIFF(dd, theDate, GetDate()) =< 7 THEN 'One Week Ago'
  WHEN DATEDIFF(dd, theDate, GetDate()) > 7 AND DATEDIFF(dd, theDate, GetDate()) < 30 THEN 'One Month Ago'
  -- ...
  END
AS TimeAgo,
Column3,
Column4
FROM Table1

有关 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:

SELECT 
Column1, 
Column2, 
theDate,
CASE
  WHEN DATEDIFF(dd, theDate, GetDate()) =< 7 THEN 'One Week Ago'
  WHEN DATEDIFF(dd, theDate, GetDate()) > 7 AND DATEDIFF(dd, theDate, GetDate()) < 30 THEN 'One Month Ago'
  -- ...
  END
AS TimeAgo,
Column3,
Column4
FROM Table1

More Information for MS SQL: http://msdn.microsoft.com/en-us/library/ms181765.aspx
(Or see the documentation for your SQL server brand)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文