2011-11-18 38 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