简体   繁体   English

如何在 SQL 中编写以下查询

[英]How to write the following Query in SQL

How to write the Query for:如何编写查询:

If I have a Table like Employee.如果我有一个像员工这样的表。 Suppose I have n rows, I want to leave top+2 rows and display from n/2+3 rows, e'g 10 rows,then display from 8th rows假设我有 n 行,我想留下 top+2 行并从 n/2+3 行显示,例如 10 行,然后从第 8 行显示

Assuming you are using SQL Server, It could help:假设您使用的是 SQL Server,它可以帮助:

SELECT  *
FROM Employee
ORDER BY Id
OFFSET 2 ROWS 
FETCH NEXT (SELECTCOUNT(1)/2 + 3 from Employee) ROWS ONLY

If MySQL, try using LIMIT like @O_Z stated.如果是 MySQL,请尝试使用 LIMIT,如@O_Z 所述。

You didn't specify your DBMS so this is standard SQL:您没有指定您的 DBMS,所以这是标准 SQL:

select *
from (
  select e.*, 
         row_number() over (order by some_column) as rn, 
         count(*) over () as total_rows
  from employee
) t
where rn in (1,2) 
   or rn >= (total_rows/2) + 3
   or rn > 8;

In MySql ,if you need different limit ranges on the same query Use union for example:在 MySql 中,如果您需要对同一查询使用不同的限制范围,请使用 union 例如:

select myCol from mytab limit 2,3
uniion 
select myCol from mytab  limit 10,40
union 
.....

In SQL Server:在 SQL Server 中:

SELECT *
FROM Employee
EXCEPT(
    SELECT TOP 2 *
    FROM Employee
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM