dti70601 2015-07-19 18:13
浏览 104
已采纳

从月开始当前月份获取mysql表中的值总和

Hi i currently have two tables, one has daily entries and other gets the total of those and stores monthly values.

I'm able to get totals of each month and year with below query, but what i'd like to do is only get the months where month is greater than and equal to current month. btw date column is in date format (yyyy-mm-dd)

$revbyMonth = $conn->prepare("SELECT EXTRACT(MONTH FROM date) as month, EXTRACT(YEAR FROM date) as year, SUM(rev) as total FROM {$tempName} GROUP BY year, month"); 
  • 写回答

1条回答 默认 最新

  • dpno17028 2015-07-19 18:16
    关注

    You want to add a where clause immediately after the from clause. I think this will work for you. If the date has no time component:

    where date > date_sub(curdate(), interval day(curdate()) day)
    

    The expression date_sub(curdate(), interval day(curdate()) day) gets the last day of the previous month. If the dates have a time component:

    where date >= date_sub(curdate(), interval day(curdate()) - 1 day)
    

    should work.

    Note: this is better than other methods that process date using functions, such as:

    where year(date) > year(curdate()) or
          (year(date) = year(curdate()) and month(date) >= month(curdate()) )
    

    The use of the functions prevents MySQL from using an index on the date column.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器