我被SQL查询困住了,我的想法是我有一个数据库,在这里我保存所有文档和每个文档version(revisionNumber)等等。我想要实现的是,我现在只想用最新的revisionNumber访问那些文档。
| id | title | documentForm | revisionNumber | effectiveDate |
| --: | ------------------- | -------------| -------------: | :------------ |
| 1 | Event Calendar | SOP-CL | 1.0 | 2011-02-02 |
| 2 | Event Calendar | SOP-CL | 2.0 | 2012-12-16 |
| 3 | Event Calendar | SOP-CL | 3.0 | 2014-02-15 |
| 4 | Event Calendar | SOP-CL | 4.0 | 2014-08-01 |
| 5 | Event Calendar | SOP-CL | 5.0 | 2016-09-12 |
| 6 | Event Calendar | SOP-CL | 6.0 | 2018-09-11 |
| 7 | Software development| SOP-DEV | 1.0 | 2015-11-25 |
| 8 | Granting and... | SOP-GRA | 1.0 | 2014-08-04 |
| 9 | Granting and... | SOP-GRA | 2.0 | 2015-12-07 |
| 10 | Granting and... | SOP-GRA | 3.0 | 2018-03-26 |在这里,您可以看到查询后需要获得的结果:
| id | title | documentForm | revisionNumber | effectiveDate |
| --: | ------------------- | ------------ | -------------: | :------------ |
| 6 | Event Calendar | SOP-CL | 6.0 | 2018-09-11 |
| 7 | Software development| SOP-CL | 1.0 | 2015-11-25 |
| 3 | Granting and... | SOP-GRA | 3.0 | 2018-03-26 |我一直在谷歌上搜索,发现可以通过分组(例如,documentForm和返回MAX(revisionNumber) )来完成,但是我没有得到正确的行id和effectiveDate。我想我只是没有正确地使用它们。
发布于 2018-09-12 06:58:46
使用相关子查询
select * from table1 t1
where revisionNumber in ( select max(revisionNumber)
from
table1 t2 where t1.title=t2.title and t1.documentForm=t2.documentForm
group by t2.title,t2.documentForm
)发布于 2018-09-12 07:00:27
您可以尝试在where子句中使用子查询。
模式(MySQL v5.6)
CREATE TABLE t (
title varchar(50),
documentForm varchar(50),
effectiveDate date,
revisionNumber int
);
insert into t values ('vent Calendar','SOP-CL','2011-02-02',1.0);
insert into t values ('vent Calendar','SOP-CL','2012-12-16',2.0);
insert into t values ('vent Calendar','SOP-CL','2014-02-15',3.0);
insert into t values ('vent Calendar','SOP-CL','2014-08-01',4.0);
insert into t values ('vent Calendar','SOP-CL','2016-09-12',5.0);
insert into t values ('vent Calendar','SOP-CL','2018-09-11',6.0);
insert into t values ('oftware development ','SOP-DEV','2015-11-25',1.0);
insert into t values ('ranting and..','SOP-GRA','2014-08-04',1.0);
insert into t values ('ranting and..','SOP-GRA','2015-12-07',2.0);
insert into t values ('ranting and..','SOP-GRA','2018-03-26',3.0);查询#1
SELECT *
FROM t t1
WHERE revisionNumber = (
select max(tt.revisionNumber)
from t tt
WHERE t1.documentForm = tt.documentForm
);
| title | documentForm | effectiveDate | revisionNumber |
| -------------------- | ------------ | ------------- | -------------- |
| vent Calendar | SOP-CL | 2018-09-11 | 6 |
| oftware development | SOP-DEV | 2015-11-25 | 1 |
| ranting and.. | SOP-GRA | 2018-03-26 | 3 |发布于 2018-09-12 06:59:05
有一个执行GROUP BY的子查询来返回每个documentForm的最高版本revisionNumber。JOIN的结果如下:
select t1.*
from tablename t1
join (select documentForm, version(revisionNumber) as maxrevisionNumber
from tablename
group by documentForm) t2
on t1.documentForm = t2.documentForm
and t1.revisionNumber = t2.maxrevisionNumberhttps://stackoverflow.com/questions/52289138
复制相似问题