Sql server 如何从sql中的选定行中选择一些行?

Sql server 如何从sql中的选定行中选择一些行?,sql-server,Sql Server,我知道它可以通过视图完成,但我不想使用视图。首先选择一些行,如下所示: select * from ( select top(3 + 6 - 1) * from mytable except select top(6 - 1) * from mytable ) as newtable /*then select from selected rows as below:*/ select id,name from newtable where condition 在

我知道它可以通过视图完成,但我不想使用视图。首先选择一些行,如下所示:

select *   from 
(
    select top(3 + 6 - 1) * from mytable
    except
    select top(6 - 1) * from mytable
) as newtable
/*then select from selected rows as below:*/
select id,name from newtable where condition

在没有视图的情况下,我如何才能做到这一点?

您的解释不是很好,但您可能想要一个CTE

With MyCTE AS (
    select *   from 
    (
        select top(3 + 6 - 1) * from mytable
        except
        select top(6 - 1) * from mytable
    ) as newtable
);

select id,name from MyCTE where condition
那么:

select id,name from (
    select *   from (select top(3 + 6 - 1) * from mytable
    except
    select top(6 - 1) * from mytable) as newtable
    ) as table2 where condition

也许你可以看到这样的线索:为什么要进行如此奇怪的硬编码计算?如果它们是常量,为什么不把它们变成常量呢。更大的问题是,您使用的是TOP,但没有orderby。实际上,此查询与前3项相同。此查询试图做什么?@SeanLange将该查询的行m设置为n。GO不是t-sql语句。它是SSMS中的默认批处理终止符。但我不确定这与问题有什么关系。我只能假设您在参数中传递这些值,或者将其构建为传递查询。仍然没有order by,无法知道您将返回哪些行。谢谢,我认为这会起作用,但我希望分两步选择。您需要两个查询的order by。他们正在选择没有order by的top,因此无法保证返回哪些行。是。如果没有order by,该查询将不会像您预期的那样“分页”。您应该添加一个,否则最终将分页到相同的记录。