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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题