[英]How to write the following Query in SQL
如何編寫查詢:
如果我有一個像員工這樣的表。 假設我有 n 行,我想留下 top+2 行並從 n/2+3 行顯示,例如 10 行,然后從第 8 行顯示
假設您使用的是 SQL Server,它可以幫助:
SELECT *
FROM Employee
ORDER BY Id
OFFSET 2 ROWS
FETCH NEXT (SELECTCOUNT(1)/2 + 3 from Employee) ROWS ONLY
如果是 MySQL,請嘗試使用 LIMIT,如@O_Z 所述。
您沒有指定您的 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;
在 MySql 中,如果您需要對同一查詢使用不同的限制范圍,請使用 union 例如:
select myCol from mytab limit 2,3
uniion
select myCol from mytab limit 10,40
union
.....
在 SQL Server 中:
SELECT *
FROM Employee
EXCEPT(
SELECT TOP 2 *
FROM Employee
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.