如何从表中选择除前5个记录之外的所有记录?

时间:2016-11-14 08:21:03

标签: sql-server sql-server-2008

我是新手在mysql中查询表并使用存储过程。我在下面有这个查询,我想查询>5的所有数据。是否存在类似select * from table Except top 5之类的查询,因此它将从数据#6开始我搜索一些代码,但我发现只是一个没有条件的直接查询。

SELECT *
    FROM dbo.PersonSkill INNER JOIN
    dbo.SpecialSkill ON dbo.PersonSkill.SpecialSkillsId = dbo.SpecialSkill.SpecialSkillsId
WHERE dbo.PersonSkill.PersonId = @PersonId          

4 个答案:

答案 0 :(得分:2)

您可以简单地使用LIMIT参数。

LIMIT 5, 10

第一个参数(5) - 从

开始

第二个参数(10) - 要选择的行数

答案 1 :(得分:2)

试试这个

SELECT     *
    FROM         dbo.PersonSkill INNER JOIN
                          dbo.SpecialSkill ON dbo.PersonSkill.SpecialSkillsId = dbo.SpecialSkill.SpecialSkillsId
    WHERE     dbo.PersonSkill.PersonId = @PersonId   and unique_id not in (SELECT   top 5  unique_id
    FROM         dbo.PersonSkill INNER JOIN
                          dbo.SpecialSkill ON dbo.PersonSkill.SpecialSkillsId = dbo.SpecialSkill.SpecialSkillsId
    WHERE     dbo.PersonSkill.PersonId = @PersonId )

答案 2 :(得分:2)

SQL Server没有LIMIT或OFFSET函数,但您可以像this一样模拟此行为,结果如下:

;WITH Results_CTE AS
(
    SELECT
        dbo.PersonSkill.*, dbo.SpecialSkill.SpecialSkillsId, dbo.SpecialSkill.OtherCol
        ROW_NUMBER() OVER (ORDER BY @PersonId) AS 'RowNum'
    FROM dbo.PersonSkill 
    INNER JOIN dbo.SpecialSkill 
    ON dbo.PersonSkill.SpecialSkillsId = dbo.SpecialSkill.SpecialSkillsId
    WHERE dbo.PersonSkill.PersonId = @PersonId 
)
SELECT *
FROM Results_CTE
WHERE RowNum >5

答案 3 :(得分:1)

试试这个

SELECT * FROM (SELECT *,ROW_NUMBER() OVER (ORDER BY @PersonId) as rn
FROM dbo.PersonSkill INNER JOIN
dbo.SpecialSkill ON dbo.PersonSkill.SpecialSkillsId = dbo.SpecialSkill.SpecialSkillsId
WHERE dbo.PersonSkill.PersonId = @PersonId)A where A.rn>5