【问题标题】:Codeigniter query to data from database for each dayCodeigniter 每天从数据库查询数据
【发布时间】:2015-03-13 17:18:04
【问题描述】:

我有一个 $from date$to date ,所以我想从数据库中获取在 $from$to 之间的几天内的四个值,包括 $from$to 。如果数据在数据库中不存在一天,则必须将零作为缺失值的值。

那么我将如何在 codeigniter 中编写一个查询来实现它,并且相应的日期应该存储在特定行的结果中。

【问题讨论】:

  • 我想这个问题会给你一个解决方案stackoverflow.com/questions/3538858/…
  • 你的数据库表的结构是什么?即使没有想要的值,您是否有每个日期的记录?
  • 不,我的记录中没有每个日期
  • 和日期,它以什么格式存储在数据库中?

标签: php mysql codeigniter


【解决方案1】:

我之前的解决方案是我使用 PHP 来检查缺失日期并将其设置为零。不过我有一些新的解决方案,你可以试试

  1. 创建一个表来存储日期并与您的表进行左/右连接
  2. 或者使用存储过程和连接创建临时表。会话过期时会自动删除临时文件
  3. 将 UNION 与 select 语句一起使用

StackOverflow 上有很多答案 MySQL how to fill missing dates in range? MySQL group by date and count including missing dates Running total over date range - fill in the missing dates MySQL to fill in missing dates when using GROUP BY DATE(table.timestamp) without joining on temporary table

【讨论】:

  • Minh Quy 我明白了,但我不想为它创建一个表我想使用现有的数据库。我的数据库中没有每个日期。
  • 我看到了你的问题。您可以创建临时表来存储$from$to 日期之间的每个日期。第二种方式,您可以使用 for 循环 PHP 创建 UNION 语句并与普通语句连接。或者使用我愚蠢的解决方案,在打印/回显结果时使用 PHP 检查丢失的日期
  • 哈哈哈..!对您的回答完全满意。我将使用第二种方法而不是愚蠢的解决方案,但它也是正确的。首先,我希望我有机会为一个人投票两次。
【解决方案2】:

我认为您的解决方案实际上需要在 PHP 中使用,并且不确定您是否可以通过查询直接从 MYSQL 获得您要查找的内容。据我了解,您想要运行查询,获取您定义的日期范围内的所有记录,然后让没有记录的日期有一个空行(或您决定的任何其他值......)。

我实际上会运行与选择日期范围之间的行相同的查询,并使用DatePeriod Class 生成一个包含开始日期和结束日期之间所有日期的数组。

$begin = new DateTime( '2012-08-01' );
$end = new DateTime( '2012-10-31' );
$end = $end->modify( '+1 day' );

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}

有了这个,我们就可以每天从$from_date跑到$end_date

接下来,我们需要从数据库中取出其他行,根据我们拥有的daterange 对象查看哪些天有记录,哪些没有记录。

我相信这是一种可行的方法,它不是最干净的示例,而是对其进行了一些额外的工作,您可以使它更漂亮一些,但我认为这将满足您的需求。

代码中的数据库部分不在 Codeigniter 中,但由于它只是获取一个简单的查询,因此更改它应该没有任何问题。

// set the start & end dates
$from_date = '2012-09-11';
$to_date     = '2012-11-11';

// create a daterange object
$begin         = new DateTime($from_date);
$end           = new DateTime($to_date );
$end            = $end->modify( '+1 day' );
$interval      = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

$sth = $dbh->prepare("SELECT col1, dateCol from tb WHERE dateCol>'".$from_date."' AND dateCol<'".$to_date."' order by dateCol ");
$sth->execute(); 
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);

$rowsByDay = array(); // will hold the rows with date keys

// loop on results to create thenew rowsByDay
foreach($rows as $key=>$row) {
    $rowsByDay[strtotime($row['dateCol'])][] = $row; // add the row to the new rows array with a timestamp key
}

// loop of the daterange elements and fill the rows array 
foreach($daterange as $date){
    if(!isset($rowsByDay[strtotime($date->format("Y-m-d"))])) // if element does not exists - meaning no record for a specific day
    {
        $rowsByDay[strtotime($date->format("Y-m-d"))] = array(); // add an empty arra (or anything else)
    }
}

// sort the rowsByDay array so they all are arrange by day from start day to end day
ksort($rowsByDay); 


// just for showing what we get at the end for rowsByDay array
foreach ($rowsByDay as $k=>$v) {
    echo date('Y-m-d',$k);
    var_dump($v);
    echo '<hr/>';
}

希望这能让你走上正确的道路......

【讨论】:

    【解决方案3】:

    希望对你有帮助,

    $this->db->where('date_column >= ',$date_given_start);
    $this->db->where('date_column <= ',$date_given_end);
    $this->db->get('TABLE_NAME')->result();
    
    
    // if column is datetime
    $this->db->where('DATE(date_column) >= ',$date_given_start);
    $this->db->where('DATE(date_column) <= ',$date_given_end);
    $this->db->get('TABLE_NAME')->result();
    

    【讨论】:

    • 对不起,我已经知道您编写的查询,但它不会返回数据库中缺失日期的零值。所以请仔细阅读我的问题并回答。
    猜你喜欢
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多