简体   繁体   English

如何在YII中上个月的两个日期之间获取数据?

[英]how to get data between two dates of Last month in YII?

I want to get data of the last month, i am using the below code to do it, but it is not working correctly for me: 我想获取上个月的数据,我正在使用下面的代码来执行此操作,但它对我而言无法正常工作:

Here is my code: 这是我的代码:

public function getLastMonth($user_id)
{       
    $criteria=new CDbCriteria;

    $criteria->condition = 'leave_from_date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)';
    $criteria->condition = 'leave_to_date <= DATE_SUB(CURDATE(), INTERVAL 1 DAY)';
    $criteria->condition = "user_id = $user_id";
    $data = Leaves::model()->findAll($criteria);
    $lastMonthR = 0;
    foreach($data as $lastMonth)
     {
         $lastMonthR += $lastMonth->total_leaves_hours;
     }      
    //return lastMonthR CHtml::encode($lastMonthR);
    return $lastMonthR;
}

I want to get the result equivalent to the results of following query that is in MYSQL: 我想得到与以下查询在MySQL中相同的结果:

SELECT *,sum(total_leaves_hours) FROM tbl_leaves
WHERE leave_from_date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND 
leave_to_date <= DATE_SUB(CURDATE(), INTERVAL 1 DAY) ;

When i run the code(shown above) of YII, it shows me all the data of the table, please help me i am new to YII. 当我运行YII的代码(如上所示)时,它向我显示了表的所有数据,请帮助我我是YII的新手。 thanks in advance. 提前致谢。

When you assign a value for condition for the second time you write over the first condition. 第二次为condition分配值时,将覆盖第一个条件。 Combine the two into a single condition. 将两者合并为一个条件。 You should also use prepared statements instead of string interpolation: 您还应该使用准备好的语句而不是字符串插值:

$criteria->condition = 'leave_from_date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
      AND leave_to_date <= DATE_SUB(CURDATE(), INTERVAL 1 DAY
      AND user_id = :id';

$data = Leaves::model()->findAll($criteria, array (':id'=>$user_id));

You wrote $criteria->condition = ... three times. 您写了$criteria->condition = ... 3次。 That assigns the last value and discards the rest. 这将分配最后一个值,并丢弃其余的值。 Joni's answer gives a simple solution. 乔尼的答案给出了一个简单的解决方案。

However, after looking closer at your logic, it can actually be replaced with a simpler SUM query, if you do not use any other data from the query. 但是,仔细查看逻辑之后, 如果您不使用查询中的任何其他数据,则实际上可以将其替换为更简单的SUM查询。 The sample query you gave selects more data, but the function you wrote does not use it - it's not clear if you actually need the records themselves. 您提供的示例查询选择了更多数据,但是您编写的函数并未使用它-尚不清楚您是否真正需要记录本身。

public function getLastMonth($user_id) {
    $cmd = Yii::app()->db->createCommand()
      ->select('SUM(total_leaves_hours)')
      ->from(Leaves::model()->tableName())
      ->andWhere('leave_from_date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)')
      ->andWhere('leave_to_date <= DATE_SUB(CURDATE(), INTERVAL 1 DAY)')
      ->andWhere('user_id = :user_id', array(
        ':user_id' => $user_id,
      ))
    ;

    return $cmd->queryScalar();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM