3

我想从搜索字符串的开始到最后一次出现字符串的子字符串。

示例字符串“Abcd Corp 是一个很好的合作组织。Abcd corp 合并”

搜索字符串“公司”

预期输出“Abcd Corp 是一个很好的合作组织。Abcd”

IE。获取字符串直到最后一次出现搜索字符串。有人可以建议最好的方法吗?

4

2 回答 2

1

Here is an way, probably not the most efficient way:


mysql> set @a = "Abcd Corp is a good org to work with. Abcd corp incorporated";
Query OK, 0 rows affected (0.00 sec)

mysql> select substring(@a, 1, length(@a) - length('corp') - instr(reverse(@a), reverse('corp')) + 1);
+-----------------------------------------------------------------------------------------+
| substring(@a, 1, length(@a) - length('corp') - instr(reverse(@a), reverse('corp')) + 1) |
+-----------------------------------------------------------------------------------------+
| Abcd Corp is a good org to work with. Abcd corp in                                      |
+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

于 2013-03-21T10:33:40.977 回答
0

您可以使用 Substring_index 函数

 select substring_index("Abcd Corp is a good org to work with. Abcd corp incorporated.", "corp", 1) 

注意:此查询仅在 Copr 在两次可用时有效。如果超过两个,则需要更改函数参数。

于 2013-03-21T08:29:38.820 回答