14

これはサンプルテーブルです:

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

表示するデータである1つの行を選択します(例:callValueのある行:val3)。しかし、これに対する解決策を見つけることができません
。前の行と次の行を選択する必要があります。したがって、この例では、行callValue:val4とcallValue:val2、またはid:5とid:12からデータを取得する必要があります。

行を削除するために連続している必要がないため、 id= id+-1では実行できません。id

4

4 に答える 4

27

これを試して:

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) 
于 2012-03-23T08:54:52.623 に答える
15

id8を取得したら、次のバリエーションを実行できるはずです。

select * from mytable
where id < 8
order by id desc
limit 1

と:

select * from mytable
where id > 8
order by id asc
limit 1

前のレコードと次のレコード。

于 2012-03-23T08:56:33.410 に答える
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 次元に展開されるため、最初のテーブルの行を 2 番目のテーブルの任意の行セットと比較できます。

于 2012-03-23T09:06:34.450 に答える
-1
select *
from callvalue
where id <  maxd
(
select max(id) as maxd
from callvalue
where id = maxd
)
于 2014-02-02T03:32:14.070 に答える