2015-10-24 38 views
-1

我想選擇第一行然後跳過X的下一行然後在一個查詢中選擇休息。例如,如果我在表格I中有(a,b,c,d,e)需要選擇「a」(第一行),則跳過X = 2行(「b」,「c」),然後選擇「 「d」和「e」,全部在一個查詢中。因此,其結果將是,d,EMySQL選擇第一行然後跳過幾個

+0

辦法之一是請選擇* from your_table colA訂購 限制1 工會全部 select * from your_table order by colA limit 3,9999999' –

+0

UNION works great – qerigan

回答

0

您可以使用一個變量來生成一個行號:

select 
    YourField, 
    YourOtherField 
from 
(
    select id, 
    YourField, 
    YourOtherField, 
    @row := @row + 1 as rownum 
    from YourTable 
    cross join (select @row:=0) c 
    order by YourField -- The field you want to sort by when you say 'first' and 'fourth' 
) d 
where 
    rownum = 1 or rownum >= 4 
1

嘗試

select * 
from 
(
    select *, @rank := @rank + 1 as rank 
    from your_table 
    cross join (select @rank := 0) r 
    order by colA 
) tmp 
where rank = 1 
or rank > 3 

select * from your_table 
order by colA 
limit 1 
union all 
select * from your_table 
order by colA 
limit 4, 9999999 
相關問題