【问题标题】:Select last week when a new year splits the week当新年分开一周时选择上周
【发布时间】:2020-04-29 05:10:34
【问题描述】:

今天是 1 月 11 日。根据我的需要,上周是从 12 月 29 日到 1 月 4 日这一周。 2019 年在星期二结束,2020 年在星期三开始。

我的程序一直给我带来各种各样的问题,因为我使用 yearweek() 来增加和减少报告的周数。

我不知道如何进行“新年”证明的查询。周数从 1 到 52 或其他任何行不通。我只需要计算从上周日到一周前(上周六)发生的任何时间段

我的 yearweek 几乎全年都可以使用,我可以对年底或年初进行自定义查询,但我希望每次都可以使用。以前的搜索没有提出任何“新年”证据。

WHERE YEARWEEK(appointment.start) = YEARWEEK(NOW(0),0)-1

今天不工作。到 2019 年底,与 +1 相同的数学运算也不起作用

这是确切的查询:

SELECT Appointment.start, Appointment.jobID, Appointment.inter_id, Appointment.subject, Appointment.location,
Appointment.outcome_id, Appointment.region, Appointment.language, Interpreters.First_Name, Interpreters.Last_Name
FROM Appointment
left join  Interpreters on Appointment.inter_id = Interpreters.inter_id
WHERE YEARWEEK(start) = YEARWEEK(NOW(0),0)-1

【问题讨论】:

  • 请显示表架构或至少提及该列的确切数据类型 :)
  • 添加了整个查询,谢谢
  • meta.stackoverflow.com/questions/333952/…。忽略任何不相关的列,在这种情况下,这些列似乎几乎是所有列。
  • 草莓你的意见很好。我将在我以后提出的任何问题中制作最少的可重现代码(第一次 - 我稍后在此处添加)

标签: mysql


【解决方案1】:

确定您的开始和结束日期是一个准备步骤,而不是您查询的一部分。您没有提到什么编程语言,所以请原谅一些猜测:PHP 有 a feature 将“上周日”转换为数字日期(然后另一个将 PHP 的日期重新格式化为 MySQL 喜欢的日期)。 Node.js 和其他语言要么具有内置的类似功能,要么可安装——例如,节点生态系统有一个 module called moment,还有一些其他语言竞争取代它。

简而言之,你:

  1. 获取您拥有的任何来源信息,以及您需要的开始和结束日期
  2. 然后将这两个日期转换成 MySQL 喜欢的格式的字符串,它(在大多数项目中)是YYYY-MM-DD 23:59:59。 (documentation for MySQL date, datetime, and timestamp types)
  3. select你需要的数据where日期字段是>=(大于或等于)你的开始日期,<=(小于或等于)你的结束日期。 (Documentation for MySQL Operators)

【讨论】:

  • 前端是php,但查询全是mysql——保存为“视图”,可以供多个程序使用。我认为我需要的是涉及星期几的东西
【解决方案2】:

我想通了,所以我将其发布给任何可能需要它的人。

我意识到 mysql 中的所有星期函数都无法满足我的需要,因此我需要根据一周中当前日期的间隔来计算星期数。所以为了使这些工作,我不得不大量使用 dayofweek() 函数。它很长,我希望我可以让它更短,但我没有看到任何其他方式,如果我想在 sql 中而不是在 php 或任何其他前端语言中完成所有功能。我想可能还有其他人和我有同样的需求,所以如果未来版本的 mysql 包含一些可以内置这些东西的功能,那就太好了。

为了得到上周和本周的减法运算,如果星期天是星期天,这会产生一种特殊的效果,因为它不是返回 1-1=0,而是返回 7。显然,他们将其编程为原因,1-7 是唯一可以取的值。因此,为了克服这个问题,我不得不使用 cast() 函数将当前日期的星期几转换为整数。

这让我这个星期天: select date_sub(curdate(), interval (dayofweek(curdate() -1)) day)

所以这让我上周日 select (date_sub(date_sub(curdate(), interval (dayofweek(curdate() -1)) day), interval 7 day))

终于到了上周六 select (date_sub(date_sub(curdate(), interval (dayofweek(curdate() -1)) day), interval 1 day))

所以我必须使用一些大于或小于或等于运算符。这是最终代码。

select Appointment.start, Appointment.subject from Appointment
where date(Appointment.start)
>=
 (date_sub(date_sub(curdate(), interval (dayofweek(curdate() -1)) day), interval 7 day))
AND date(Appointment.start)
<=
 (date_sub(date_sub(curdate(), interval (dayofweek(curdate() -1)) day), interval 1 day))  
ORDER BY `Appointment`.`start` DESC

我将这些保存为视图,因此对于任何想要使用该代码并在 google 上找到它的人,这里是每个视图的完整代码,我通过在我的系统上手动设置不同的日期来确认这些代码有效。

查看名称:last_week_billable_jobs

select `Calendar`.`Appointment`.`start` AS `start`,`Calendar`.`Appointment`.`jobID` AS `jobID`,`Calendar`.`Appointment`.`inter_id` AS `inter_id`,`Calendar`.`Appointment`.`subject` AS `subject`,`Calendar`.`Appointment`.`location` AS `location`,`Calendar`.`Appointment`.`is_confirmed` AS `is_confirmed`,`Calendar`.`Appointment`.`is_authorized` AS `is_authorized`,`Calendar`.`Appointment`.`outcome_id` AS `outcome_id`,`Calendar`.`Appointment`.`region` AS `region`,`Calendar`.`Appointment`.`language` AS `language`,`Calendar`.`Interpreters`.`First_Name` AS `First_Name`,`Calendar`.`Interpreters`.`Last_Name` AS `Last_Name` from (`Calendar`.`Appointment` left join `Calendar`.`Interpreters` on((`Calendar`.`Appointment`.`inter_id` = `Calendar`.`Interpreters`.`inter_id`))) where ((cast(`Calendar`.`Appointment`.`start` as date) >= (curdate() - interval (cast(dayofweek(curdate()) as signed) + 6) day)) and (cast(`Calendar`.`Appointment`.`start` as date) <= (curdate() - interval (cast(dayofweek(curdate()) as signed) + 0) day)) and (`Calendar`.`Appointment`.`outcome_id` < 8)) order by `Calendar`.`Appointment`.`start`

视图名称:this_week_billable_jobs:

select `Calendar`.`Appointment`.`start` AS `start`,`Calendar`.`Appointment`.`jobID` AS `jobID`,`Calendar`.`Appointment`.`inter_id` AS `inter_id`,`Calendar`.`Appointment`.`subject` AS `subject`,`Calendar`.`Appointment`.`location` AS `location`,`Calendar`.`Appointment`.`outcome_id` AS `outcome_id`,`Calendar`.`Appointment`.`is_confirmed` AS `is_confirmed`,`Calendar`.`Appointment`.`is_authorized` AS `is_authorized`,`Calendar`.`Appointment`.`region` AS `region`,`Calendar`.`Appointment`.`language` AS `language`,`Calendar`.`Interpreters`.`First_Name` AS `First_Name`,`Calendar`.`Interpreters`.`Last_Name` AS `Last_Name` from (`Calendar`.`Appointment` left join `Calendar`.`Interpreters` on((`Calendar`.`Appointment`.`inter_id` = `Calendar`.`Interpreters`.`inter_id`))) where ((cast(`Calendar`.`Appointment`.`start` as date) >= (curdate() - interval (cast(dayofweek(curdate()) as signed) - 1) day)) and (cast(`Calendar`.`Appointment`.`start` as date) <= (curdate() - interval (cast(dayofweek(curdate()) as signed) - 7) day)) and (`Calendar`.`Appointment`.`outcome_id` < 8)) order by `Calendar`.`Appointment`.`start`

视图名称:next_week_billable_jobs:

select `Calendar`.`Appointment`.`start` AS `start`,`Calendar`.`Appointment`.`jobID` AS `jobID`,`Calendar`.`Appointment`.`inter_id` AS `inter_id`,`Calendar`.`Appointment`.`subject` AS `subject`,`Calendar`.`Appointment`.`location` AS `location`,`Calendar`.`Appointment`.`outcome_id` AS `outcome_id`,`Calendar`.`Appointment`.`region` AS `region`,`Calendar`.`Appointment`.`is_confirmed` AS `is_confirmed`,`Calendar`.`Appointment`.`is_authorized` AS `is_authorized`,`Calendar`.`Appointment`.`language` AS `language`,`Calendar`.`Interpreters`.`First_Name` AS `First_Name`,`Calendar`.`Interpreters`.`Last_Name` AS `Last_Name` from (`Calendar`.`Appointment` left join `Calendar`.`Interpreters` on((`Calendar`.`Appointment`.`inter_id` = `Calendar`.`Interpreters`.`inter_id`))) where ((cast(`Calendar`.`Appointment`.`start` as date) >= (curdate() + interval (8 - dayofweek(curdate())) day)) and (cast(`Calendar`.`Appointment`.`start` as date) <= (curdate() + interval (14 - dayofweek(curdate())) day)) and (`Calendar`.`Appointment`.`outcome_id` < 8)) order by `Calendar`.`Appointment`.`start`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2017-06-20
    • 2018-09-18
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多