【问题标题】:Find the name, start date and duration in months of projects that have the earliest end date - mysql查找具有最早结束日期的项目的名称、开始日期和持续时间 - mysql
【发布时间】:2021-02-21 11:31:06
【问题描述】:

我正在研究 sql 中的子查询,但无法弄清楚这个问题的答案。以下是表格。

项目(p͟r͟o͟j͟n͟o͟, projname, prestdate, prendate)

+--------+----------------------+------------+------------+
| projno | projname             | prstdate   | prendate   |
+--------+----------------------+------------+------------+
| AD3100 | Admin Services       | 2014-01-01 | 2015-02-01 |
| AD3110 | General AD Systems   | 2014-01-01 | 2015-02-01 |
| MA2113 | W L Prod Cont Progs  | 2014-02-15 | 2014-12-01 |
| PL2100 | Weld Line Planning   | 2014-01-01 | 2014-09-15 |

我想出了这个,但我认为它是错误的:

select projname
     , prstdate
     , MONTH(prendate - prstdate) as duration 
  from Proj 
 where prendate - prstdate IN (select MIN(prendate - prstdate) from Proj);

【问题讨论】:

  • 哪个项目的结束日期最早?或者,更准确地说,最早的结束日期是什么时候?
  • @Strawberry 只是结束日期最早的项目。由于英语不是我的第一语言,我也很难准确理解这个问题。
  • 那么您认为开始日期在确定结束日期方面起什么作用?

标签: mysql sql subquery


【解决方案1】:

你在正确的轨道上。但是您的查询是查看项目的持续时间,而不是结束日期

其次,关闭时长的计算。最好使用的函数是TIMESTAMPDIFF()

因此,只需修改代码以查看结束日期而不是持续时间:

select projnamek, prstdate
       timestampdiff(month, prstdate, prendate) as duration 
from Proj 
where prendate = (select min(prendate) from Proj);

【讨论】:

    猜你喜欢
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多