SQL 除了按描述顺序排列的前 10 条记录

发布于 2024-09-18 22:52:57 字数 532 浏览 4 评论 0原文

我想提取语句中除前 10 条之外的所有记录,但在子查询“关键字 ORDER 附近的语法不正确”中使用 ORDER BY 时,我不断遇到问题

@ID INT
as
SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar

FROM table.miComments JOIN table.mbrProfile2 ON MID = MemberId 

WHERE VID = @ID EXCEPT (SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar FROM table.miComments JOIN table.mbrProfile2 ON MID = MemberId 
WHERE VID = @ID ORDER BY UdateAdded DESC) 'ERROR: Incorrect Syntax near the keyword ORDER'

ORDER BY UdateAdded DESC

I want to pull all records except the TOP 10 in my statement but I keep running into problems when using ORDER BY in my subquery "Incorrect Syntax near the keyword ORDER"

@ID INT
as
SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar

FROM table.miComments JOIN table.mbrProfile2 ON MID = MemberId 

WHERE VID = @ID EXCEPT (SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar FROM table.miComments JOIN table.mbrProfile2 ON MID = MemberId 
WHERE VID = @ID ORDER BY UdateAdded DESC) 'ERROR: Incorrect Syntax near the keyword ORDER'

ORDER BY UdateAdded DESC

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

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

发布评论

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

评论(4

木緿 2024-09-25 22:52:57

如果您使用的是 MS SQL Server,则没有 LIMIT 或 OFFSET 等效项,因此您必须使用子查询来完成您想要的操作。

SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar 
FROM table.miComments 
JOIN table.mbrProfile2 ON MID = MemberId  
WHERE VID = @ID AND ComVID NOT IN (
   SELECT TOP 10 ComVID
   FROM table.miComments 
   JOIN table.mbrProfile2 ON MID = MemberId  
   WHERE VID = @ID ORDER BY UdateAdded DESC)  
ORDER BY UdateAdded DESC 

以下 StackOverflow 链接包含更多信息,可能会有所帮助:

LIMIT 10..20在 SQL Server 中

If you're using MS SQL Server, there is no LIMIT or OFFSET equivalent, so you'd have to use a subquery to accomplish what you want.

SELECT ComVID, VID, MID, Ucomment, UdateAdded, MemberId, UserName, Avatar 
FROM table.miComments 
JOIN table.mbrProfile2 ON MID = MemberId  
WHERE VID = @ID AND ComVID NOT IN (
   SELECT TOP 10 ComVID
   FROM table.miComments 
   JOIN table.mbrProfile2 ON MID = MemberId  
   WHERE VID = @ID ORDER BY UdateAdded DESC)  
ORDER BY UdateAdded DESC 

The following StackOverflow link has a lot more information in it, which may be helpful:

LIMIT 10..20 in SQL Server

微凉徒眸意 2024-09-25 22:52:57

您正在谈论偏移。如果您有一个返回行 1,2,3,4,5,6,7 的查询,并且您想跳过前 3 行(产生 4,5,6,7 code>),您可以指定偏移量 3。

在 MySQL 中,您可以使用接受偏移量参数的 LIMIT 子句。在 PostgreSQL 中,您需要 OFFSET 子句。 SQLServer(截至我上次被迫使用它)不支持偏移量。

PostgreSQL

OFFSET 表示在开始返回行之前跳过那么多行。 OFFSET 0 与省略 OFFSET 子句相同。如果同时出现 OFFSET 和 LIMIT,则在开始计算返回的 LIMIT 行之前会跳过 OFFSET 行。

SELECT * FROM tbl OFFSET 10

MySQL

在 MySQL 中,如果不指定偏移量,就无法指定偏移量。由于某些奇怪的原因而受到限制:

要检索从某个偏移量到结果集末尾的所有行,可以使用较大的数字作为第二个参数。此语句检索从第 96 行到最后一行的所有行:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

You're talking about an offset. If you have a query returning rows 1,2,3,4,5,6,7 and you want to skip over the first 3 (yielding 4,5,6,7), you can specify an offset of 3.

In MySQL you can use the LIMIT clause which accepts an offset argument. In PostgreSQL you'll want the OFFSET clause. SQLServer (as of the last time I was forced to use it) doesn't support offsets.

PostgreSQL

OFFSET says to skip that many rows before beginning to return rows. OFFSET 0 is the same as omitting the OFFSET clause. If both OFFSET and LIMIT appear, then OFFSET rows are skipped before starting to count the LIMIT rows that are returned.

SELECT * FROM tbl OFFSET 10

MySQL

In MySQL you cannot specify an offset without also specifying a limit for some bizarre reason:

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;
情魔剑神 2024-09-25 22:52:57

我目前无法重现您确切的 SQL 配置,但要回答“一般问题”:

  • 选择所有数据
  • 选择前 10 名数据
  • 将 EXCEPT 放在两个 SELECT 查询之间

示例:

SELECT
    SUM(s.Records)
FROM
(
    -- List of ALL records
    SELECT
        dbo.people.name,
        dbo.people.address,
        Count (*) as Records
    FROM 
        dbo.people
    WHERE
        dbo.people.registration_date >= '01/01/2013'
        AND
        dbo.people.registration_date < '01/01/2014'
    GROUP BY
        dbo.people.name, dbo.people.address

    -- Exclusion
    EXCEPT

    -- TOP 10 list
    SELECT TOP 10
        dbo.people.name,
        dbo.people.address,
        Count (*) as Records
    FROM 
        dbo.people
    WHERE
        dbo.people.registration_date >= '01/01/2013'
        AND
        dbo.people.registration_date < '01/01/2014'
    GROUP BY
        dbo.people.name, dbo.people.address
    ORDER BY
        COUNT (*) DESC
) s

假设我们有一个人员及其姓名 + 的数据库地址组合将它们限定为唯一记录(不是在关系级别上,而是在业务需求级别上)。

EXCEPT 自 SQL Server 2005 起可用。

I can't reproduce your exact SQL configuration at this moment, but to answer the "general question":

  • Select ALL data
  • Select TOP 10 data
  • Put EXCEPT in between the two SELECT queries

Example:

SELECT
    SUM(s.Records)
FROM
(
    -- List of ALL records
    SELECT
        dbo.people.name,
        dbo.people.address,
        Count (*) as Records
    FROM 
        dbo.people
    WHERE
        dbo.people.registration_date >= '01/01/2013'
        AND
        dbo.people.registration_date < '01/01/2014'
    GROUP BY
        dbo.people.name, dbo.people.address

    -- Exclusion
    EXCEPT

    -- TOP 10 list
    SELECT TOP 10
        dbo.people.name,
        dbo.people.address,
        Count (*) as Records
    FROM 
        dbo.people
    WHERE
        dbo.people.registration_date >= '01/01/2013'
        AND
        dbo.people.registration_date < '01/01/2014'
    GROUP BY
        dbo.people.name, dbo.people.address
    ORDER BY
        COUNT (*) DESC
) s

Assuming we have a database of people and their Name + Address combination qualifies them as a unique record (not on a relational level, but on a business requirements level).

EXCEPT has been available since SQL Server 2005.

江挽川 2024-09-25 22:52:57

您可以将其用作简单的查询:

with CTP as (select top 10 id_site from sites order by id_site DESC)
select * from sites
where id_site not in (select id_site from CTP)

You can use this as a simple query :

with CTP as (select top 10 id_site from sites order by id_site DESC)
select * from sites
where id_site not in (select id_site from CTP)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文