2011-11-18 93 views
0

这应该是简单的,但我在细节迷路....MySQL的定位和反向查询

在数据库领域,如果字符串包含一个字“世纪”,我想前面的词,例如'16th'

我一直在尝试使用LOCATE和REVERSE函数,但无法使其工作。谁能帮忙?

谢谢!

回答

1

你可以尝试:

使用SUBSTRING_INDEX之前 '世纪' 返回字符串。
反向使用反转字符串。
使用substring_index在第一个空格之前返回字符串(可能需要修剪它?)。
反向使用以再次反转字符串。

+1

谢谢!作为参考,我最终使用的SQL是:concat(REVERSE(SUBSTRING_INDEX(REVERSE(TRIM(SUBSTRING_INDEX(1case(FREETEXT_DETAIL_EN),'century',1))),'',1)),'','century' ) – JezB

0
 
mysql> select substring_index('what century now', 'century', 1); 
+---------------------------------------------------+ 
| substring_index('what century now', 'century', 1) | 
+---------------------------------------------------+ 
| what            | 
+---------------------------------------------------+ 
1 row in set (0.00 sec) 

mysql> select substring_index('what centuries now', 'century', 1); 
+-----------------------------------------------------+ 
| substring_index('what centuries now', 'century', 1) | 
+-----------------------------------------------------+ 
| what centuries now         | 
+-----------------------------------------------------+ 
1 row in set (0.00 sec) 

mysql> select substring_index('what century now century', 'century', 1); 
+-----------------------------------------------------------+ 
| substring_index('what century now century', 'century', 1) | 
+-----------------------------------------------------------+ 
| what              | 
+-----------------------------------------------------------+ 
1 row in set (0.00 sec) 
+0

谢谢,但选择substring_index('这是从现在的16世纪','世纪',1);返回'这是从现在的16世纪',这不是我所追求的。 – JezB

+0

'百年<>世纪',如果你想允许复数搜索,是另一个故事 – ajreal