这是示例表:
Column | 1st record | 2nd record | 3rd record | 4th record | etc<br />
id (primary) | 1 | 5 | 8 | 12 | etc<br />
name | name 1 | name 2 | name 3 | name 4 | etc<br />
date | date 1 | date 2 | date 3 | date 4 | etc<br />
callValue (unique) | val1 | val2 | val3 | val4 | etc
我选择一行作为要显示的数据(例如:具有callValue的行:val3)。但我无法找到解决方案:
我需要选择上一行和下一行。所以,在这个例子中,我需要从行vallValue:val4和callValue:val2,或id:5和id:12获取数据。
id
= id
+ - 1无法完成,因为id
因删除行而不必连续。
答案 0 :(得分:26)
试试这个:
select * from test where callValue = 'val3'
union all
(select * from test where callValue < 'val3' order by id desc limit 1)
union all
(select * from test where callValue > 'val3' order by id asc limit 1)
或
select * from test where id = 8
union all
(select * from test where id < 8 order by id desc limit 1)
union all
(select * from test where id > 8 order by id asc limit 1)
答案 1 :(得分:12)
获得身份8
后,您应该可以对以下内容进行修改:
select * from mytable
where id < 8
order by id desc
limit 1
和
select * from mytable
where id > 8
order by id asc
limit 1
表示上一张和下一张唱片。
答案 2 :(得分:2)
这有效:
select a.id, a.name, a.date, a.callValue
FROM
(
select @r0 := @r0 + 1 as rownum, id, name, date, callValue from TABLE
, (SELECT @r0 := 0) r0
) a,
(
select @r1 := @r1 + 1 rownum, id, name, date, callValue from TABLE
, (SELECT @r1 := 0) r1
) b
where b.callValue = val3 and b.rownum between (a.rownum-1 and a.rownum+1)
它将表扩展为2维,因此您可以将第一个表中的行与第二个表中的任何行进行比较。
答案 3 :(得分:-1)
select *
from callvalue
where id < maxd
(
select max(id) as maxd
from callvalue
where id = maxd
)