MySQL选择第一行然后跳过几个

时间:2015-10-24 17:31:28

标签: mysql select

我正在尝试选择第一行,然后跳过X下一行,然后在一个查询中选择rest。例如,如果我在表中有(a,b,c,d,e),我需要选择“a”(第一行)然后跳过X = 2行(“b”,“c”),然后选择rest,这是“d”和“e”,都在一个查询中。结果将是a,d,e

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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