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

SQL,按描述顺序排列的前10条记录除外,sql,select,Sql,Select,我想提取语句中除前10条之外的所有记录,但在子查询中使用ORDERBY时,在关键字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 Co

我想提取语句中除前10条之外的所有记录,但在子查询中使用ORDERBY时,在关键字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
你说的是补偿。如果查询返回第1、2、3、4、5、6、7行,并且希望跳过前3行,生成4、5、6、7,则可以指定偏移量3

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

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

在MySQL中,由于某些奇怪的原因,如果不指定限制,就不能指定偏移量:

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


如果您使用的是MS SQL Server,则没有限制或偏移量等效项,因此您必须使用子查询来完成所需的操作

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链接包含更多信息,可能会有所帮助:


目前我无法重现您的确切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
假设我们有一个人员数据库,并且他们的姓名+地址组合使他们成为唯一的记录,而不是关系级别的记录,而是业务需求级别的记录


除了自SQL Server 2005以来一直可用之外。

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

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)

表中的主键是什么?除了前10条记录之外,你是否试图获取所有VID=@ID的记录?我的错。我复制/粘贴了错误的陈述。除了选择前10名。。。我正在使用SQL 2005@ID INT作为SELECT ComVID,VID,MID,Ucomment,UdateAdded,MemberId,UserName,Avatar FROM.miComments JOIN table.mbrProfile2 ON MID=MemberId,其中VID=@ID除了选择前10名ComVID,VID,MID,Ucomment,UdateAdded,MemberId,UserName,MID=MemberId上的table.miComments JOIN table.mbrProfile2的化身,其中VID=@ID ORDER BY UdateAdded DESC'错误:关键字ORDER附近的语法不正确'ORDER BY UdateAdded DESC'主键是table.miComments中的covid
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
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)