首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择上个星期,新的一年将在这个星期分开。

选择上个星期,新的一年将在这个星期分开。
EN

Stack Overflow用户
提问于 2020-01-12 02:43:31
回答 2查看 60关注 0票数 0

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

我的程序给我带来了各种各样的问题,因为我用“年度周”()来增加和减去报告的周数。

我不知道如何做一个“新年”证明的问题。每周从1到52或其他什么都不起作用。我只需要计算一下从上周日到一周前(上周六)发生的任何一段时间。

我的年鉴几乎全年都能用,我可以在年底或年初进行自定义查询,但我宁愿每次都有一些有用的东西。以前的搜索还没有找到任何“新年份”的证据。

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

今天不工作。在2019年年底,+1的数学也不起作用

以下是准确的查询:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-12 05:19:08

我想出来了,所以我把这个发给可能需要它的人。

我意识到mysql中的每周函数都不能满足我的需要,所以我需要根据一周中的每一天的间隔来计算周数。因此,为了使这些工作,我必须广泛使用的一周中的日()功能。它很长,我希望我可以缩短它,但我没有看到任何其他方式,如果我想用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))

因此,我必须使用一些大于或小于或等于运算符。这是最后的代码。

代码语言:javascript
复制
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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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`
票数 0
EN

Stack Overflow用户

发布于 2020-01-12 03:22:26

确定开始日期和结束日期是准备步骤,而不是查询的一部分。您没有提到是哪种编程语言,所以请原谅一些猜测: PHP让一个特性将“上星期日”转换为数字日期(然后另一种将PHP格式转换为MySQL会喜欢的日期)。Node.js和其他语言要么内置了类似的特性,要么可以安装--例如,节点生态系统有一个moment,还有一些其他语言争相取代它。

简单地说,你:

  1. 获取您所拥有的任何源信息,用于所需的起始日期和结束日期。
  2. 然后将这两个日期转换为按MySQL喜欢的方式格式化的字符串,这(在大多数项目中)是YYYY-MM-DD 23:59:59。(MySQL日期、日期和时间戳类型的文档)
  3. select您需要的数据日期字段是>= (大于或等于)您的开始日期,<= (小于或等于)您的结束日期。(MySQL操作符文档)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59700515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档