计算并集的第二页结果

发布于 2024-11-07 08:14:30 字数 1838 浏览 0 评论 0原文

我遇到了 SQLServer 问题。

我正在尝试弄清楚如何显示第二页结果。使用下面的查询提取第一页结果。

SELECT TOP 10 * FROM (
    SELECT c.id, c.name, c.inserteddate, c.cityname, ftblstates.name AS statename, clc.name AS catname, '' AS listingimagelogo, '' AS orgname, relocateyn, '' AS employerclassified
    FROM tblclassifieds c
    LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id
    LEFT JOIN ftblstates ON c.stateid = ftblstates.id
    WHERE c.expirydate != '' AND c.expirydate >= '5/17/2011'
    UNION ALL
    SELECT ce.id, ce.name, ce.inserteddate, suburb AS cityname, ftblstates.name AS statename, ce.jobtype AS catname, ce.listingimagelogo, ce.orgname, '' AS relocateyn, '1' AS employerclassified
    FROM tblclassifiedemployers ce
    LEFT JOIN ftblstates ON ce.stateid = ftblstates.id
    WHERE ce.expirydate != '' AND ce.expirydate >= '5/17/2011'
) a
ORDER BY inserteddate desc;

答案是否与下面类似?

SELECT TOP 10 * FROM (
    SELECT c.id, c.name, c.inserteddate, c.cityname, ftblstates.name AS statename, clc.name AS catname, '' AS listingimagelogo, '' AS orgname, relocateyn, '' AS employerclassified
    FROM tblclassifieds c
    LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id
    LEFT JOIN ftblstates ON c.stateid = ftblstates.id
    WHERE c.expirydate != '' AND c.expirydate >= '5/17/2011'
    UNION ALL
    SELECT ce.id, ce.name, ce.inserteddate, suburb AS cityname, ftblstates.name AS statename, ce.jobtype AS catname, ce.listingimagelogo, ce.orgname, '' AS relocateyn, '1' AS employerclassified
    FROM tblclassifiedemployers ce
    LEFT JOIN ftblstates ON ce.stateid = ftblstates.id
    WHERE ce.expirydate != '' AND ce.expirydate >= '5/17/2011'
) a

WHERE NOT a.id IN (SELECT TOP 10 tblclassifieds.id ...

ORDER BY inserteddate desc;

有人可以帮我完成查询吗?

I've got a SQLServer problem.

I'm trying to work out how to show a second page of results. The first page of results are pulled using the query below.

SELECT TOP 10 * FROM (
    SELECT c.id, c.name, c.inserteddate, c.cityname, ftblstates.name AS statename, clc.name AS catname, '' AS listingimagelogo, '' AS orgname, relocateyn, '' AS employerclassified
    FROM tblclassifieds c
    LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id
    LEFT JOIN ftblstates ON c.stateid = ftblstates.id
    WHERE c.expirydate != '' AND c.expirydate >= '5/17/2011'
    UNION ALL
    SELECT ce.id, ce.name, ce.inserteddate, suburb AS cityname, ftblstates.name AS statename, ce.jobtype AS catname, ce.listingimagelogo, ce.orgname, '' AS relocateyn, '1' AS employerclassified
    FROM tblclassifiedemployers ce
    LEFT JOIN ftblstates ON ce.stateid = ftblstates.id
    WHERE ce.expirydate != '' AND ce.expirydate >= '5/17/2011'
) a
ORDER BY inserteddate desc;

Is the answer similar to the following?

SELECT TOP 10 * FROM (
    SELECT c.id, c.name, c.inserteddate, c.cityname, ftblstates.name AS statename, clc.name AS catname, '' AS listingimagelogo, '' AS orgname, relocateyn, '' AS employerclassified
    FROM tblclassifieds c
    LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id
    LEFT JOIN ftblstates ON c.stateid = ftblstates.id
    WHERE c.expirydate != '' AND c.expirydate >= '5/17/2011'
    UNION ALL
    SELECT ce.id, ce.name, ce.inserteddate, suburb AS cityname, ftblstates.name AS statename, ce.jobtype AS catname, ce.listingimagelogo, ce.orgname, '' AS relocateyn, '1' AS employerclassified
    FROM tblclassifiedemployers ce
    LEFT JOIN ftblstates ON ce.stateid = ftblstates.id
    WHERE ce.expirydate != '' AND ce.expirydate >= '5/17/2011'
) a

WHERE NOT a.id IN (SELECT TOP 10 tblclassifieds.id ...

ORDER BY inserteddate desc;

Can someone please help me out completing the query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2024-11-14 08:14:30

一个简单的方法:

declare @page int = 2
declare @take int = 10

select * from (
    SELECT *, ROW_NUMBER() OVER(ORDER BY inserteddate desc) as rowno FROM (
        SELECT c.id, c.name, c.inserteddate, c.cityname, ftblstates.name AS statename, clc.name AS catname, '' AS listingimagelogo, '' AS orgname, relocateyn, '' AS employerclassified
        FROM tblclassifieds c
        LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id
        LEFT JOIN ftblstates ON c.stateid = ftblstates.id
        WHERE c.expirydate != '' AND c.expirydate >= '5/17/2011'
        UNION ALL
        SELECT ce.id, ce.name, ce.inserteddate, suburb AS cityname, ftblstates.name AS statename, ce.jobtype AS catname, ce.listingimagelogo, ce.orgname, '' AS relocateyn, '1' AS employerclassified
        FROM tblclassifiedemployers ce
        LEFT JOIN ftblstates ON ce.stateid = ftblstates.id
        WHERE ce.expirydate != '' AND ce.expirydate >= '5/17/2011'
    ) a
) b
where   rowno > (@page - 1) * @take 
    AND rowno <= @page * @take
order by rowno

我没有改变你的内部查询。

编辑:在 where 子句中添加括号

A simple way of doing it:

declare @page int = 2
declare @take int = 10

select * from (
    SELECT *, ROW_NUMBER() OVER(ORDER BY inserteddate desc) as rowno FROM (
        SELECT c.id, c.name, c.inserteddate, c.cityname, ftblstates.name AS statename, clc.name AS catname, '' AS listingimagelogo, '' AS orgname, relocateyn, '' AS employerclassified
        FROM tblclassifieds c
        LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id
        LEFT JOIN ftblstates ON c.stateid = ftblstates.id
        WHERE c.expirydate != '' AND c.expirydate >= '5/17/2011'
        UNION ALL
        SELECT ce.id, ce.name, ce.inserteddate, suburb AS cityname, ftblstates.name AS statename, ce.jobtype AS catname, ce.listingimagelogo, ce.orgname, '' AS relocateyn, '1' AS employerclassified
        FROM tblclassifiedemployers ce
        LEFT JOIN ftblstates ON ce.stateid = ftblstates.id
        WHERE ce.expirydate != '' AND ce.expirydate >= '5/17/2011'
    ) a
) b
where   rowno > (@page - 1) * @take 
    AND rowno <= @page * @take
order by rowno

I didnt alter your internal query.

Edit: Added parenteses in where clause

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文