我想要复制的行为就像 grep with-A
and -B
旗帜。
例如grep -A 2 -B 2 "hello" myfile.txt
会给我所有包含“hello”的行,还有它之前的两行和之后的两行。
让我们假设这个表模式:
+--------+-------------------------+
| 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 |
+------------+---------------------+
现在查询如下: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 |
- 为简单起见,假设 'like %TERM%' 仅匹配 1 row 。
- 这里的结果应该是自动递增排序的
id
field.
是的,这对我有用:
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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)