MySQL在行前选择 [英] MySQL select before after row
本文介绍了MySQL在行前选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是示例表:
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中获取数据.
I select one row that is the data to show (for example: row with callValue: val3). But I cannot find a solution for this:
I need to select previous and next row. So, in this example, I need to get data from rows vallValue: val4 and callValue: val2, or id: 5 and id: 12.
使用id
= id
+-1不能完成此操作,因为由于删除行,id
不必是连续的.
It cannot be done with id
=id
+-1 because id
doesn't have to be continuous because of deleting rows.
推荐答案
尝试一下:
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)
这篇关于MySQL在行前选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文