在具有最早结束日期的项目的月份中查找名称、开始日期和持续时间 - mysql
Find the name, start date and duration in months of projects that have the earliest end date - mysql
我在研究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);
你走在正确的轨道上。但是您的查询是查看项目的 duration,而不是 end date。
第二,持续时间的计算是关闭的。最好使用的函数是 TIMESTAMPDIFF()
.
因此,只需修改代码以查看结束日期而不是持续时间:
select projnamek, prstdate
timestampdiff(month, prstdate, prendate) as duration
from Proj
where prendate = (select min(prendate) from Proj);
我在研究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);
你走在正确的轨道上。但是您的查询是查看项目的 duration,而不是 end date。
第二,持续时间的计算是关闭的。最好使用的函数是 TIMESTAMPDIFF()
.
因此,只需修改代码以查看结束日期而不是持续时间:
select projnamek, prstdate
timestampdiff(month, prstdate, prendate) as duration
from Proj
where prendate = (select min(prendate) from Proj);