简体   繁体   English

使MySQL查询动态化的最佳方法(例如,更新查询参数以显示上周)

[英]Best method for making a MySQL query dynamic (e.g., update query parameters to show Previous Week)

I have a query which calculates the total hours worked for the current week by task: 我有一个查询,它按任务计算当前工作周的总工作时数:

SELECT name AS 'Task Name'
       , sum(hours) AS `Total Hours`
        FROM time_records
        WHERE yearweek(record_date, 3) = yearweek(now(), 3)
        GROUP BY
        weekday(record_date), name

The WHERE clause specifies the current week: WHERE子句指定当前周:

WHERE yearweek(record_date, 3) = yearweek(now(), 3)

This data is displayed as a table using the MySQL query and a PDO statement. 此数据使用MySQL查询和PDO语句显示为表。

I would now like to add an option for the viewer to see the previous week of data by clicking a link under the table (eg, "Previous Week"). 我现在想通过单击表格下的链接(例如,“上周”)为查看者添加一个选项,以查看前一周的数据。 In order to show that data, I would need to update the WHERE clause to query for the previous week: 为了显示该数据,我需要更新WHERE子句以查询前一周:

   WHERE yearweek(record_date, 3) = yearweek(now(), 3) - 1

I'm wondering what the best way to do this is? 我想知道最好的方法是什么? I would prefer to keep just one version of the overall query. 我宁愿只保留整个查询的一个版本。 One thought is to use PHP to add the "- 1" to the query, such as: 一种想法是使用PHP在查询中添加“ - 1”,例如:

WHERE yearweek(record_date, 3) = yearweek(now(), 3) <?php if (prev) {echo "- 1" ;} ?>

I think this should work, but how can I determine the prev condition? 我认为这应该有效,但我怎样才能确定现状? Should I make the entire query be inside a function so that for the default it can be "Time_hours(0)" and for the previous it can be "Time_hours(1)" - or is there some better way? 我应该将整个查询放在一个函数中,以便默认情况下它可以是“Time_hours(0)”,对于之前的它可以是“Time_hours(1)” - 或者是否有更好的方法?

I'm concerned about keeping the code manageable and following best practices. 我担心保持代码可管理并遵循最佳实践。 Any tips would be appreciated. 任何提示将不胜感激。 Thanks! 谢谢!

I suggest you try a query that has a parameter for the number of weeks ago you want to process. 我建议您尝试一个查询,其中包含您要处理的周数之前的参数。 YEARWEEK is a bad choice for weekly processing because it handles end-of-year rollovers poorly. YEARWEEK是每周处理的糟糕选择,因为它很难处理年终翻滚。

Try this, if your weeks begin on Sunday. 如果您的周数从周日开始,请尝试这样做。 This will give the current week. 这将是本周。

select r.record_id, r.record_date, w.week weekstart
from record r
join (
    select 
      (FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7)) 
           - interval 0 week) week
 )w
where record_date >= w.week
  and record_date < w.week + interval 1 week

Fiddle: http://sqlfiddle.com/#!2/4c487/1/0 小提琴: http ://sqlfiddle.com/#!2/4c487/1/0

If you want, let's say, one week ago, change - interval 0 week to - interval 1 week in your query. 如果你想要,比如说,一周前,在你的查询中改变- interval 0 week - interval 1 week See this fiddle. 看到这个小提琴。 http://sqlfiddle.com/#!2/4c487/2/0 http://sqlfiddle.com/#!2/4c487/2/0

You can put an an arbitrary number of weeks ago, even weeks that span year ends. 您可以在几周之前放置一个任意数字,甚至是跨越年末的几周。 See here for interval -42 week for example. 例如,请参阅此处的interval -42 week http://sqlfiddle.com/#!2/4c487/3/0 http://sqlfiddle.com/#!2/4c487/3/0

The expression FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7)) rounds the current date down to the previous Sunday. 表达式FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))将当前日期向下FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))到上一个星期日。 If you want to round it to the previous Monday, use FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -2, 7)) . 如果要将其舍入到上一个星期一,请使用FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -2, 7)) For example: http://sqlfiddle.com/#!2/4c487/8/0 例如: http//sqlfiddle.com/#!2/4c487/8/0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM