【问题标题】:Count of records for the last and current month unix timestamp上个月和当前月 unix 时间戳的记录计数
【发布时间】:2015-10-11 21:10:30
【问题描述】:

我需要使用自动查询为计费制作统计页面。例如,现在是 7 月,查询必须显示 6 月的记录计数和当天(仅 7 月)的当前记录计数。排序:

"上个月的记录 - 85,当天的记录 - 32"

我有表customer 和行create_time,但它是unix 时间戳。试过了

customer 中选择计数(*)create_time >= UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 1 MONTH))

但这绝对不是我想要的。

我将不胜感激。

【问题讨论】:

    标签: mysql select count unix-timestamp


    【解决方案1】:

    您可以通过向 WHERE 添加条件来做到这一点:

    create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 month))
    

    在 PHP 中你可以计算前一个月的日期

    $newDate = strtotime('-1 month');
    

    并在查询中使用它

    【讨论】:

      【解决方案2】:

      试试这个:

      SELECT *
        FROM ( SELECT COUNT(*) AS total_previous_month
                 FROM customer
                WHERE create_time >= concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01')
                  AND create_time < LAST_DAY(now() - interval 1 month )
             ) AS s1,
             ( SELECT COUNT(*) AS total_previous_month
                 FROM customer
                WHERE create_time >= concat(extract(year from now()),'-',extract(month from now()),'-01')
                  AND create_time <= now()
             ) AS s2
      

      【讨论】:

        猜你喜欢
        • 2021-10-18
        • 1970-01-01
        • 2015-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多