简体   繁体   中英

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

Assuming you are using SQL Server, It could help:

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.

You didn't specify your DBMS so this is standard 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:

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

In SQL Server:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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