【问题标题】:Select all the records of the previous month in Zend Framework?在 Zend Framework 中选择上个月的所有记录?
【发布时间】:2011-07-11 01:03:00
【问题描述】:

我的项目有一个小需求:

我想fetch all the records of the previous month from the database.

structure of the table如下:

id   clientid     task   date     
1    1            1      01.Feb.2011 12:13
2    1            1      05.Feb.2011 15:22
3    1            0      09.Feb.2011 14:17
4    2            1      11.Feb.2011 19:53
5    1            0      19.Feb.2011 14:27
6    2            1      23.Feb.2011 09:53
7    1            0      01.Mar.2011 14:17
8    2            1      01.Mar.2011 19:53
9    1            0      03.Mar.2011 14:67
10   2            1      03.Mar.2011 09:53
.....................

这里我想fetchparticular clientprevious monthrecordsZend Framework.中的所有records

例如:如果我想要client 1 记录,那么它应该显示记录:1,2,3 and 5.

请推荐一些代码,或者链接对我有帮助......

提前致谢

【问题讨论】:

    标签: php mysql zend-framework datetime


    【解决方案1】:

    假设日期列是DateTime column,我会尝试类似

    $select->from('tablename')
           ->where('MONTH(date) = ?', date('n', strtotime('last month')))
           ->where('YEAR(date) = ?', date('Y'))
           ->where('clientid = ?', $clientId)
    ;
    

    注意:未经测试,可能需要调整,但这是大方向

    这将从表名中获取所有行,其中月份是上个月,年份是当前年份,并且您的 clientId 是选定的 clientId。所以查询应该变成类似

    SELECT * from tablename 
        WHERE MONTH(date) = 2 
          AND YEAR(date) = 2011
          AND clientid = 1;
    

    您也可以将上个月和当年的计算直接放入查询中,例如为此使用appropriate MySql functions 而不是用PHP 计算它们。这可能更可靠。

    【讨论】:

      【解决方案2】:

      您可以使用 PHP 获取当月的第一天:

      $this_month = mktime(0, 0, 0, date("m"), 1, date("Y"));
      $previous_month = mktime(0, 0, 0, date("m")-1, 1, date("Y"));
      

      然后您只需将此日期作为查询参数传递:

      SELECT * FROM mytable WHERE date >= ? AND date < ? and client_id = 1
      

      你在哪里替换?分别按 '$previous_month' 和 '$this_month'

      【讨论】:

      • 这也将返回早于 1 个月前的记录。
      【解决方案3】:

      如果您的 date 字段是 Datetime 类型,您可以使用 MySQL 中的日期特定函数来执行此操作。使用数据库函数时,只需使用Zend_Db_Expr 构造您的语句。

      【讨论】:

        【解决方案4】:

        我的 Zend_Db 技能有点生疏,但我认为以下内容可以满足您的要求:

        $select->from('tablename')
                   ->where(new Zend_Db_Expr('MONTH(`date`) =  MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH))'))
                   ->where(new Zend_Db_Expr('YEAR(`date`) = IF(MONTH(NOW()) = 1, YEAR(NOW()) - 1, YEAR(NOW()))'))
                   ->where('clientid = ?', $clientId)
        

        【讨论】:

          【解决方案5】:

          您可以使用 SQL DATE_SUB 和 INTERVAL 函数,例如:

          select * from table where `date` >=  DATE_SUB(NOW(), INTERVAL 1 month)
          

          在 ZF1 中你可以这样写:

          $select->from('tablename')
           ->where( 'date >=  DATE_SUB(NOW(), INTERVAL 1 month)');
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-09-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多