我有两个表作为tblDocument,tblDocVersions使用主键docid连接如下:
tblDocument :docid | title
tblVersions :id | docid | verion | datecreated | status
tblVersions可以将数据作为
id + docid + version
----+-------+--------+
1 | 1 | 1
----+-------+---------
2 | 1 | 2
----+-------+---------
3 | 1 | 3
----+-------+---------
4 | 2 | 1
----+-------+---------
5 | 3 | 1
----+-------+---------
6 | 3 | 2
----+-------+---------
现在我需要用最新版本选择每个单独的文档......
注意:我需要从Versions
中选择与最新版本号相对应的行。
答案 0 :(得分:2)
select max(version), tab1.docid from tblDocument as tab1
inner join tblVersions as tab2 on tab1.docid = tab2.docid
group by tab1.docid
修改强>
不确定但这对您有用
select tab1.* from tblDocument as tab1
inner join tblVersions as tab2 on tab1.docid = tab2.docid
inner join
(select max(version) as ver,tab1.docid from tblDocument group by tab1.docid ) d
on d.docid = tab1.docid and d.ver = tab1.version