日期:2014-05-16  浏览次数:20679 次

如何根据搜索到的字符串的行返回前3行的值

ID1                       ID2
1                       xxxxxxx
2                       user   admin
3                       xxxxxxx
4                       xxxxxxx
5                       xxxxxxx
6                       user   root
7                       xxxxxxx
8                       xxxxxxx
9             230   Login   successful
10                     xxxxxxx
11                     xxxxxxx


检索表字段ID2,只要遇到“230   Login   successful”就返回以上第3个字符串,比如以上,9是“230   Login   successful”,那么6就是需要返回的字符串
这个SQL如何写呢?



------解决方案--------------------
select * from newtable where id1 in (select (case when id2= '230 Login successful ' then id1 -3 else 0 end) as id2 from newtable where id2 <> 0)
上面的这个需要id1 大于3。不过按照你的要求,小于3就不符合了。