donglv1831 2014-05-30 15:14
浏览 35
已采纳

使用php和mysql检索上个月插入的值

Okay, So I am needing to retrieve the rows in a database table that where inserted the previous month of the current. shop_orders is the database.

I do have a datetime in each row for the order date or o_date and that will be used to determine when it was entered.

I came up with this function in codeigniter model. The first portion in the code determines what the first day of last month is and puts into the same format that is stored in the database. The second portion retrieves the information from the database. I'm just not sure what to place in the where line to retrieve the results only from the first day of last month to the last day of last month.

The first portion of the below code outputs just like this:

Today is: 2014/05/30 the first day of last month was: 2014/04/01 and the last day of last month was: 2014/04/30.

I need to tell the second block:

Select * from shop_orders where o_date is between 2014/04/01 and 2014/04/30

Obviously that isn't correct sql, what is? My real question.

function get_last_month_order_count()
{
    $date = date('Y/m/d');
    $today = new DateTime( $date );
    $today->modify( 'first day of last month' );
    $firstDay = $today->format( 'Y/m/d' );
    $today = new DateTime( $date );
    $today->modify( 'last day of last month' );
    $lastDay = $today->format( 'Y/m/d' );

    $this->db->select('*');
    $this->db->from('shop_orders');
    $this->db->where('o_date', '');

    return $this->db->count_all_results();
}
  • 写回答

1条回答 默认 最新

  • duangou1551 2014-05-30 15:20
    关注
    SELECT * FROM shop_orders WHERE (date_field BETWEEN ' . $firstDay . ' AND ' . $lastDay . ');
    

    Or for you this should work:

    $this->db->select('*');
    $this->db->from('shop_orders');
    $this->db->where('o_date >=', $first_date);
    $this->db->where('o_date <=', $second_date);
    

    Alternatively you should be able to do:

    $this->db->select('*');
    $this->db->from('shop_orders');
    $this->db->where('o_date BETWEEN "' . $first_date . '" AND "' . $second_date . '"');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 鸿业暖通修改详细负荷时闪退
  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体