2015-10-24 37 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 
相关问题