3

假设我有两张书籍表格和两张相应版本的表格。

我有一个查询如下:

SELECT TOP 10 * FROM
(SELECT hbID, hbTitle, hbPublisherID, hbPublishDate, hbedID, hbedDate
 FROM hardback
 LEFT JOIN hardbackEdition on hbID = hbedID
 UNION 
 SELECT pbID, pbTitle, pbPublisher, pbPublishDate, pbedID, pbedDate
 FROM paperback
 Left JOIN paperbackEdition on pbID = pbedID
) books
WHERE hbPublisherID = 7
ORDER BY hbPublishDate DESC

如果前两本精装本和/或平装本有 5 个版本,则此查询仅返回两本书。但是,我希望TOP 10仅适用于返回的实际图书记录数。有没有办法我可以选择 10 本书,并且仍然可以获得所有相关的版本记录?

如果它是相关的,我没有创建和删除临时表的数据库权限。

谢谢阅读!

更新

澄清一下:平装表有一个相关的平装版本表。精装表有一个相关的精装版本表。除了将(希望!)看到它们一起显示的用户之外,精装本和平装本表彼此不相关。

4

3 回答 3

1

如果我理解正确,您可以通过以下方式获得所有相关版本的 10 本书

  • 使用WITH语句返回初始的完整结果集
  • 使用 a 选择 10 本书GROUP BY
  • JOIN该组的结果保留了来自给定 10 本书的所有信息。

SQL 语句

;WITH books AS (
  SELECT  hbID, hbTitle, hbPublisherID, hbPublishDate, hbedID, hbedDate
  FROM    hardback
          LEFT JOIN hardbackEdition on hbID = hbedID
  WHERE   hbPublisherID = 7          
  UNION ALL
  SELECT  pbID, pbTitle, pbPublisher, pbPublishDate, pbedID, pbedDate
  FROM    paperback
          LEFT JOIN paperbackEdition on pbID = pbedID
  WHERE   hbPublisherID = 7
)
SELECT  *
FROM    books b
        INNER JOIN (
          SELECT TOP 10 hbID
          FROM   books
          GROUP BY
                hbID
        ) bt ON bt.hbID = b.hbID

或者如果你更喜欢只写一次 where 子句

;WITH books AS (
  SELECT  hbID, hbTitle, hbPublisherID, hbPublishDate, hbedID, hbedDate
  FROM    hardback
          LEFT JOIN hardbackEdition on hbID = hbedID
  UNION ALL
  SELECT  pbID, pbTitle, pbPublisher, pbPublishDate, pbedID, pbedDate
  FROM    paperback
          LEFT JOIN paperbackEdition on pbID = pbedID
)
, q AS (
  SELECT  *
  FROM    books
  WHERE   hbPublisherID = 7          
)
SELECT  *
FROM    q b
        INNER JOIN (
          SELECT TOP 10 hbID
          FROM   q
          GROUP BY
                hbID
        ) bt ON bt.hbID = b.hbID
于 2012-04-16T11:40:50.977 回答
0

没那么容易。您只需将 Top 10 应用于精装本和平装本表,无需连接。然后将结果加入数据。

以下查询仅在 hbID 和 pbID 始终唯一时有效。如果不是,它会变得更复杂。您需要将它们分开或在查询中添加另一列以区分它们。

SELECT *
FROM
  (SELECT hbID as id, hbTitle, hbPublisherID, hbPublishDate, hbedID, hbedDate
   FROM hardback
   LEFT JOIN hardbackEdition on hbID = hbedID
   UNION 
   SELECT pbID as id, pbTitle, pbPublisher, pbPublishDate, pbedID, pbedDate
   FROM paperback
   Left JOIN paperbackEdition on pbID = pbedID
  ) books
INNER JOIN 
  (SELECT TOP 10 * 
  FROM
    (SELECT hbID as id, hbPublisherID as publishedId, hbPublishDate as publishDate
     FROM hardback
     UNION 
     SELECT pbID as id, pbPublisherID as publishedId, pbPublishDate as publishDate
     FROM paperback
    ) 
  WHERE publisherID = 7
  ORDER BY publishDate DESC
  ) topTen 
  on books.id = TopTen.id
于 2012-04-16T08:24:26.253 回答
0

这应该从出版商 7 中获取最近出版的十个带有精装本的书:

select  *
from    (
        select  top 10 title
        from    hardback
        where   hbPublisherID = 7
        group by
                title
        order by
                hbPublishDate desc
        ) top_titles
left join
      hardback
on    hardback.hbTitle = top_titles.title
left join
      paperback
on    paperback.pbTitle = top_titles.title
于 2012-04-16T08:25:19.223 回答