-2

我只能获取具有最高 cvID 值的行吗?

当前代码

SELECT 
  CollectionVersionBlocks.cID,
  CollectionVersionBlocks.cbDisplayOrder,
  CollectionVersionBlocks.cvID,
  btContentLocal.bID,
  btContentLocal.content 
FROM
  CollectionVersionBlocks 
  INNER JOIN btContentLocal 
    ON CollectionVersionBlocks.bID = btContentLocal.bID 
WHERE (CollectionVersionBlocks.cID = 259)
  AND CollectionVersionBlocks.isOriginal = 1 
  AND CollectionVersionBlocks.arHandle = 'main' 
  AND btContentLocal.content != '' 

我想获取底部的行(其中 cvID 值为 10)。

这是针对更大结果集的测试语句 -

我最终将需要一组来自 perset cID 的结果(CollectionVersionBlocks.cID = 259 OR CollectionVersionBlocks.cID = 260 ... 最多 800)

结果图片

更新的屏幕截图

1) 太少的结果 2) 未分组的结果

结果2

在此处输入图像描述

4

2 回答 2

2

要获得每组的最高行(从您的问题我假设cID为一个组),您可以通过在您的第三个连接中使用附加条件在所需列的最大值上使用自连接来实现,即ON(c.cID=cc.cID AND c.cvID=cc.cvID)

SELECT 
  c.cID,
  c.cbDisplayOrder,
  c.cvID,
  b.bID,
  b.content 
FROM
  CollectionVersionBlocks  c
  INNER JOIN btContentLocal b
    ON (c.bID = b.bID)
  INNER JOIN 
  (SELECT cID, MAX(cvID) cvID FROM CollectionVersionBlocks GROUP BY cID) cc
  ON(c.cID=cc.cID AND c.cvID=cc.cvID)
WHERE (c.cID = 259)
  AND c.isOriginal = 1 
  AND c.arHandle = 'main' 
  AND b.content != ''

对于多个组,您可以使用WHERE c.cID IN(259,....800)

于 2014-04-30T12:28:04.090 回答
0

试试下面的查询:

SELECT CollectionVersionBlocks.cID,CollectionVersionBlocks.cbDisplayOrder,        CollectionVersionBlocks.cvID ,  btContentLocal.bID , btContentLocal.content 
FROM CollectionVersionBlocks
INNER JOIN btContentLocal
ON CollectionVersionBlocks.bID=btContentLocal.bID 
WHERE (CollectionVersionBlocks.cID = 259) 
AND CollectionVersionBlocks.isOriginal=1 AND CollectionVersionBlocks.arHandle ='main'            AND           btContentLocal.content !='' and CollectionVersionBlocks.cID in
(

    SELECT Max(CollectionVersionBlocks.cID)
    FROM CollectionVersionBlocks
    INNER JOIN btContentLocal
    ON CollectionVersionBlocks.bID=btContentLocal.bID 
    WHERE (CollectionVersionBlocks.cID = 259) 
    AND CollectionVersionBlocks.isOriginal=1 AND CollectionVersionBlocks.arHandle   ='main' AND btContentLocal.content !='' )
于 2014-04-30T11:57:48.353 回答