SQL Query仅获取存在一对多关系的记录的一个实例

时间:2013-04-21 03:49:22

标签: sql sql-server

我有一个SQL查询试图在存在1对多关系的情况下获得1条记录。

SELECT     dbo.BlogEntries.ID AS blog_entries_id, dbo.BlogEntries.BlogTitle, dbo.BlogEntries.BlogEntry, dbo.BlogEntries.BlogName, 
                      dbo.BlogEntries.DateCreated AS blog_entries_datecreated, dbo.BlogEntries.inActive AS blog_entries_in_active, 
                      dbo.BlogEntries.HtmlMetaDescription AS blog_entries_html_meta_description, dbo.BlogEntries.HtmlMetaKeywords AS blog_entries_html_meta_keywords, 
                      dbo.BlogEntries.image1, dbo.BlogEntries.image2, dbo.BlogEntries.image3, dbo.BlogEntries.formSelector, dbo.BlogEntries.image1Alignment, 
                      dbo.BlogEntries.image2Alignment, dbo.BlogEntries.image3Alignment, dbo.BlogEntries.blogEntryDisplayName, dbo.BlogEntries.published AS blog_entries_published, 
                      dbo.BlogEntries.entered_by, dbo.BlogEntries.dateApproved, dbo.BlogEntries.approved_by, dbo.blog_entry_tracking.id AS blog_entry_tracking_id, 
                      dbo.blog_entry_tracking.blog, dbo.blog_entry_tracking.blog_entry, dbo.BlogCategories.ID, dbo.BlogCategories.BlogCategoryName, 
                      dbo.BlogCategories.BlogCategoryComments, dbo.BlogCategories.DateCreated, dbo.BlogCategories.BlogCategoryTitle, dbo.BlogCategories.BlogCategoryTemplate, 
                      dbo.BlogCategories.inActive, dbo.BlogCategories.HtmlMetaDescription, dbo.BlogCategories.HtmlMetaKeywords, dbo.BlogCategories.entry_sort_order, 
                      dbo.BlogCategories.per_page, dbo.BlogCategories.shorten_page_content, dbo.BlogCategories.BlogCategoryDisplayName, dbo.BlogCategories.published, 
                      dbo.BlogCategories.blogParent
FROM         dbo.BlogEntries LEFT OUTER JOIN
                      dbo.blog_entry_tracking ON dbo.BlogEntries.ID = dbo.blog_entry_tracking.blog_entry LEFT OUTER JOIN
                      dbo.BlogCategories ON dbo.blog_entry_tracking.blog = dbo.BlogCategories.ID

我将一些记录分配给2个不同的blogcategories,当我查询所有内容时,它会返回重复的记录。

我如何只返回1个博客实例?

3 个答案:

答案 0 :(得分:1)

试试这个 -

SELECT  blog_entries_id = be.Id
    ,   be.BlogTitle
    ,   be.BlogEntry
    ,   be.BlogName
    ,   blog_entries_datecreated = be.DateCreated
    ,   blog_entries_in_active = be.inActive
    ,   blog_entries_html_meta_description = be.HtmlMetaDescription
    ,   blog_entries_html_meta_keywords = be.HtmlMetaKeywords
    ,   be.image1
    ,   be.image2
    ,   be.image3
    ,   be.formSelector
    ,   be.image1Alignment
    ,   be.image2Alignment
    ,   be.image3Alignment
    ,   be.blogEntryDisplayName
    ,   blog_entries_published = be.published
    ,   be.entered_by
    ,   be.dateApproved
    ,   be.approved_by
    ,   blog_entry_tracking_id = bet.Id
    ,   bet.blog
    ,   bet.blog_entry
    ,   bc2.Id
    ,   bc2.BlogCategoryName
    ,   bc2.BlogCategoryComments
    ,   bc2.DateCreated
    ,   bc2.BlogCategoryTitle
    ,   bc2.BlogCategoryTemplate
    ,   bc2.inActive
    ,   bc2.HtmlMetaDescription
    ,   bc2.HtmlMetaKeywords
    ,   bc2.entry_sort_order
    ,   bc2.per_page
    ,   bc2.shorten_page_content
    ,   bc2.BlogCategoryDisplayName
    ,   bc2.published
    ,   bc2.blogParent
FROM dbo.BlogEntries be
LEFT JOIN dbo.blog_entry_tracking bet ON be.Id = bet.blog_entry
OUTER APPLY (
    SELECT TOP 1 *
    FROM dbo.BlogCategories bc
    WHERE bet.blog = bc.Id
) bc2

另外,我想提一下,在这种情况下,在列名中使用别名可以减少查询的大小,使其更便于理解。

答案 1 :(得分:0)

如果您只需要一条记录,可以使用

SELECT TOP 1 dbo.BlogEntries.ID AS blog_entries_id,dbo.Bl ....(与现在相同)。

它比SELECT DISTINCT

更有效

答案 2 :(得分:0)

这是Northwind示例。

每个订单的订单明细表中只返回1行。

Use Northwind
GO

Select COUNT(*) from dbo.Orders
select COUNT(*) from dbo.[Order Details] 


select * from dbo.Orders ord
join
(select ROW_NUMBER() OVER(PARTITION BY OrderID ORDER BY UnitPrice DESC) AS "MyRowID" , * from dbo.[Order Details] innerOD) derived1
on ord.OrderID = derived1.OrderID
Where
derived1.MyRowID = 1
Order by ord.OrderID