SQL查询除外,然后选择TOP 10

时间:2018-11-23 08:56:57

标签: sql sql-server except

我有以下查询,其中我除Microsoft SQL Server中的两个结果外

SELECT *
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (1)

EXCEPT

SELECT *
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (2)

ORDER BY News.PublishDate DESC

结果有很多记录,但是我只想要前10个结果。我该怎么做?

5 个答案:

答案 0 :(得分:0)

使用LIMIT

Select *
FROM
(
SELECT *
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (1)
ORDER BY News.PublishDate DESC

EXCEPT

SELECT *
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (2)
ORDER BY News.PublishDate DESC
)a
LIMIT 10;

答案 1 :(得分:0)

在使用SQL Server时,请使用top关键字

select top 10 t.* from 
(
SELECT *
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (1)

EXCEPT

SELECT *
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (2)
) t order by t.PublishDate desc

答案 2 :(得分:0)

这就是@Damien_The_Unbeliever和@Mayank Porwal的帮助。

Select TOP(10) *
FROM
(
SELECT News.Author, News.CreateDate, News.CustomerId, News.EditDate, News.EndDate, News.Id, News.ImageUrl, News.PublishDate, News.Text, News.Title
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (1)

EXCEPT

SELECT News.Author, News.CreateDate, News.CustomerId, News.EditDate, News.EndDate, News.Id, News.ImageUrl, News.PublishDate, News.Text, News.Title
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (2)

) a

ORDER BY a.PublishDate DESC

答案 3 :(得分:0)

这将起作用:

SELECT TOP 10 * from  (SELECT *
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (1)

EXCEPT

SELECT *
FROM News
INNER JOIN NewsAssignment on News.Id = NewsAssignment.NewsId
INNER JOIN NewsAudience on NewsAssignment.NewsAudienceId = NewsAudience.Id
WHERE NewsAudience.PortalId IN (2)

ORDER BY News.PublishDate DESC);

答案 4 :(得分:0)

您的查询没有任何意义。您将从第一个子查询获得 all 行。为什么?因为您正在使用select *,并且NewsAudience.PortalId在选择列表中。该值会根据子查询中的条件自动不同。

我建议您以不同的方式表述您的需求。例如,您可能打算:

SELECT n.*
FROM News n
WHERE EXISTS (SELECT 1
              FROM NewsAssignment na JOIN
                   NewsAudience nau
                   ON na.NewsAudienceId = nau.Id AND na.PortalId IN (1)
              WHERE n.Id = na.NewsId
             ) AND
      NOT EXISTS (SELECT 1
                  FROM NewsAssignment na JOIN
                       NewsAudience nau
                       ON na.NewsAudienceId = nau.Id AND na.PortalId IN (2)
                  WHERE n.Id = na.NewsId
                 )
ORDER BY n.PublishDate DESC;

如果在正确的逻辑上仍然存在重复问题,则可以在外部查询中添加TOP (10)