检索最近两天的记录

时间:2013-04-25 07:05:20

标签: mysql codeigniter

如何获取最近两天的记录是我的查询,但收到错误。

function getbdologHistoryByRefManuals($obj)
{
    $this->db->select('bdo_logs.*,transaction_statuses.status');
    $this->db->from('bdo_logs');
    $this->db->join('transactions','transactions.id=bdo_logs.id');
    $this->db->join('transaction_statuses','transaction_statuses.id=transactions.trans_status_id');
    $this->db->where('log_date >', ADDDATE( CURDATE( ) , INTERVAL -2 DAY ));

    $query = $this->db->get();

    //echo $this->db->last_query();
    if($query->num_rows() == 0){
        return null;
    }

            return $query->result();
}

2 个答案:

答案 0 :(得分:2)

function getbdologHistoryByRefManuals($obj)
{
    $this->db->select('bdo_logs.*,transaction_statuses.status');
    $this->db->from('bdo_logs');
    $this->db->join('transactions','transactions.id=bdo_logs.id');
    $this->db->join('transaction_statuses','transaction_statuses.id=transactions.trans_status_id');
    $this->db->where('DATE(log_date) >', 'ADDDATE( CURDATE( ) , INTERVAL -2 DAY )');

    $query = $this->db->get();

    if($query->num_rows() == 0){
        return null;
    }

    return $query->result();
}

您缺少在where子句

中包装ADDDATE函数

编辑:
或者你可以这样称呼它

$query  ="  SELECT
                bdo_logs.*,
                transaction_statuses.status
            FROM bdo_logs
            JOIN transactions 
                ON transactions.id  =   bdo_logs.id
            JOIN transaction_statuses 
                ON transaction_statuses.id  =   transactions.trans_status_id
            WHERE DATE(log_date) > ADDDATE( CURDATE( ) , INTERVAL -2 DAY )";
$result = $this->db->query($query); 

答案 1 :(得分:2)

简单方法:

$today = date('Y-m-d');
$newdate = date('Y-m-d', strtotime("-2 days"));
echo $today;
echo '<br />';
echo $newdate;

现在,将$newdate置于where条件中。