我想要复制的行为就像grep with -A
和-B
flags.例如,它grep -A 2 -B 2 "hello" myfile.txt
会给我所有在其中有"你好"的行,但也包括前面的2行和后面的2行.让我们假设这个表模式:
+--------+-------------------------+
| id | message |
+--------+-------------------------+
| 1 | One tow three |
| 2 | No error in this |
| 3 | My testing message |
| 4 | php module test |
| 5 | hello world |
| 6 | team spirit |
| 7 | puzzle game |
| 8 | social game |
| 9 | stackoverflow |
|10 | stackexchange |
+------------+---------------------+
Run Code Online (Sandbox Code Playgroud)
现在像这样的查询
Select * from theTable where message like '%hello%'
将导致:
5 | hello world
如何在之前选择N行的另一个参数"N"和匹配记录之后的N行,即N = 2,结果应为:
| 3 | My testing message |
| 4 | php module test |
| 5 | hello world |
| 6 | team spirit |
| 7 | puzzle game |
Run Code Online (Sandbox Code Playgroud)
id
字段上排序.Daa*_*mer 12
对,这对我有用:
SELECT child.*
FROM stack as child,
(SELECT idstack FROM stack WHERE message LIKE '%hello%') as parent
WHERE child.idstack BETWEEN parent.idstack-2 AND parent.idstack+2;
Run Code Online (Sandbox Code Playgroud)
不知道这是不是有效的MySQL,但是怎么样
SELECT t.*
FROM theTable t
INNER JOIN (
SELECT id FROM theTable where message like '%hello%'
) id ON id.id <= t.id
ORDER BY
ID DESC
LIMIT 3
UNION ALL
SELECT t.*
FROM theTable t
INNER JOIN (
SELECT id FROM theTable where message like '%hello%'
) id ON id.id > t.id
ORDER BY
ID
LIMIT 2
Run Code Online (Sandbox Code Playgroud)
试试这个简单的(编辑) -
CREATE TABLE messages(
id INT(11) DEFAULT NULL,
message VARCHAR(255) DEFAULT NULL
);
INSERT INTO messages VALUES
(1, 'One tow three'),
(2, 'No error in this'),
(3, 'My testing message'),
(4, 'php module test'),
(5, 'hello world'),
(6, 'team spirit'),
(7, 'puzzle game'),
(8, 'social game'),
(9, 'stackoverflow'),
(10, 'stackexchange');
SET @text = 'hello world';
SELECT id, message FROM (
SELECT m.*, @n1:=@n1 + 1 num, @n2:=IF(message = @text, @n1, @n2) pos
FROM messages m, (SELECT @n1:=0, @n2:=0) n ORDER BY m.id
) t
WHERE @n2 >= num - 2 AND @n2 <= num + 2;
+------+--------------------+
| id | message |
+------+--------------------+
| 3 | My testing message |
| 4 | php module test |
| 5 | hello world |
| 6 | team spirit |
| 7 | puzzle game |
+------+--------------------+
Run Code Online (Sandbox Code Playgroud)
N值可以指定为用户变量; 目前它是 - '2'.
此查询适用于行号,这可以保证返回最近的记录.
归档时间: |
|
查看次数: |
11437 次 |
最近记录: |