0

我正在使用 mysql 数据库。

sbs=> 从 docbodyversion 中选择 *;

 bodyid | internaldocid | versionid
--------+---------------+-----------
   1121 |          1245 |         1
   1122 |          1245 |         2
   1124 |          1246 |         1
   1125 |          1246 |         2
   1126 |          1246 |         3

我想写一个这样的查询,这样 o/p 就会像这样:

 bodyid | internaldocid | versionid
--------+---------------+-----------
   1122 |          1245 |         2
   1126 |          1246 |         3

所以我只想要每条记录的最大版本号....

有人可以指导我吗?

4

3 回答 3

1

try this:

select d.*
from docbodyversion d
join
    (select internaldocid,MAX(versionid) as versionid
     from docbodyversion
     group by internaldocid)a
on a.versionid=d.versionid
and a.internaldocid=d.internaldocid
于 2012-08-09T09:07:00.883 回答
1

尝试:

SELECT MAX(bodyId) AS BodyID,internaldocid, MAX(versionid) AS VersionID 
FROM docbodyversion GROUP BY internaldocid

看这个小提琴

于 2012-08-09T09:04:59.620 回答
0

希望这可以帮助


SELECT DISTINCT BT.BODY_ID, A.INTERNAL_DOC_ID, A.VERSION_ID
  FROM SO_BUFFER_TABLE_16 BT,
       (SELECT DISTINCT BT.INTERNAL_DOC_ID, MAX(BT.VERSION_ID) VERSION_ID
          FROM SO_BUFFER_TABLE_16 BT
         GROUP BY BT.INTERNAL_DOC_ID) A
 WHERE BT.VERSION_ID = A.VERSION_ID
   AND BT.INTERNAL_DOC_ID = A.INTERNAL_DOC_ID

于 2012-08-09T09:08:10.453 回答