Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql获取sortindex前后的行_Mysql_Sql - Fatal编程技术网

mysql获取sortindex前后的行

mysql获取sortindex前后的行,mysql,sql,Mysql,Sql,我有一张这样的桌子: id sortindex 1 15 2 3 4 1 5 4 8 6 11 2 13 5 我想在sortindex前后得到这一行 例如: 我已经给了身份证5所以sortindex 4 该表应按sortindex ASC排序 现在,我想在这之前得到这场争吵 应返回: id sortindex 2 3 id sortindex 13 5 那之后的争吵呢 应返回: id sortinde

我有一张这样的桌子:

id    sortindex
1     15
2     3
4     1
5     4
8     6
11    2
13    5
我想在sortindex前后得到这一行

例如:

我已经给了身份证5所以sortindex 4 该表应按sortindex ASC排序

现在,我想在这之前得到这场争吵 应返回:

id    sortindex
2     3
id    sortindex
13    5
那之后的争吵呢 应返回:

id    sortindex
2     3
id    sortindex
13    5
我必须如何构建查询的? 我想要两个查询:一个用于之前,一个用于之后

SELECT * FROM table1 WHERE sortindex < 4 ORDER BY sortindex DESC LIMIT 1
UNION ALL
SELECT * FROM table1 WHERE sortindex > 4 ORDER BY sortindex ASC LIMIT 1