在符合条件的行之前和之后选择N行?

Dhr*_*hak 15 mysql

我想要复制的行为就像grep with -A-Bflags.例如,它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)
  • 为简单起见,假设'like%TERM%'仅匹配1行.
  • 这里的结果应该在自动增量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)

  • 如果id之间存在间隙,这可能不起作用,否则它将是一个简单的解决方案. (9认同)

Lie*_*ers 6

不知道这是不是有效的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)


Dev*_*art 6

试试这个简单的(编辑) -

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'.

此查询适用于行号,这可以保证返回最近的记录.