Codeigniter 每天查询数据库中的数据
Codeigniter query to data from database for each day
我有 $from date
和 $to date
,所以我想从数据库中获取 $from
和 $to
之间几天的四个值,包括 $from
和 $to
。如果数据在数据库中一天不存在,则必须将零作为缺失值的值。
那么我将如何在 codeigniter 中编写查询来实现它,并且相应的日期应该存储在特定行的结果中。
希望这对您有所帮助,
$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();
我以前的解决方案是我使用 PHP 检查缺失日期并将其设置为零。但我有一些新的解决方案,你可以试试
- 创建一个 table 来存储日期并 left/right 加入您的 table
- 或使用存储过程创建临时 table 并加入。临时会在会话过期时自动删除
- 将 UNION 与 select 语句一起使用
Whosebug 上有很多答案
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
我认为您的解决方案实际上需要在 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/>';
}
希望这能让你走上正确的道路...
我有 $from date
和 $to date
,所以我想从数据库中获取 $from
和 $to
之间几天的四个值,包括 $from
和 $to
。如果数据在数据库中一天不存在,则必须将零作为缺失值的值。
那么我将如何在 codeigniter 中编写查询来实现它,并且相应的日期应该存储在特定行的结果中。
希望这对您有所帮助,
$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();
我以前的解决方案是我使用 PHP 检查缺失日期并将其设置为零。但我有一些新的解决方案,你可以试试
- 创建一个 table 来存储日期并 left/right 加入您的 table
- 或使用存储过程创建临时 table 并加入。临时会在会话过期时自动删除
- 将 UNION 与 select 语句一起使用
Whosebug 上有很多答案 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
我认为您的解决方案实际上需要在 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/>';
}
希望这能让你走上正确的道路...