0

当存在一对多关系时,我有一个 SQL 查询试图取回 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 个不同的博客类别,当我查询所有内容时,它返回重复记录。

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

4

3 回答 3

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

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

于 2013-04-22T08:14:25.187 回答
0

这是一个罗斯文示例。

它将在每个订单的订单详细信息表中仅返回 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
于 2013-04-21T09:46:45.340 回答
0

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

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

它比 SELECT DISTINCT 更有效

于 2013-04-21T05:43:18.723 回答