duanlie2709 2018-03-04 23:09
浏览 68

如何使用mysql数据库中的日期字段获取每周记录

I want to select weekly and monthly records from MySQL database without specifying a start and end date, but by using the first date a record was entered into the database and calculating the sum, 7 days afterwards.

What I want to achieve is every 7 days the cost field in my application must reset to 0 and with each day calculating, until it gets to the 7th day and the cycle starts again.

I have tried the DATE_ADD and DATE_SUB function and both don't work to my expectation.

I have researched and tried many different solutions but they all don't seem to work There is sample of my queries I have tried.

Solution 1

select expense_name, expense_date, cost from expenses 
where week(expense_date)

Solution 2

select expense_name, expense_date, cost from expenses 
where yearweek(expense_date)

Sample Data

Name    Date       Cost 
Food1   2018-02-23  14
Food2   2018-02-23  8
Food3   2018-02-27  10
Food4   2018-02-27  10
Food5   2018-02-27  50
Food6   2018-03-1   2.6
Food7   2018-03-8   18

Thanks

  • 写回答

1条回答 默认 最新

  • dongyou8087 2018-03-05 01:13
    关注

    Am traveling atm but I think this will kinda help you.

    Not sure what column name you use for saving the creation date but for now I use “created_at”.

    WHERE created_at BETWEEN created_ar AND DATE_ADD( created_add, INTERVAL +1 MONTH)

    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败