這應該是簡單的,但我在細節迷路....MySQL的定位和反向查詢
在數據庫領域,如果字符串包含一個字「世紀」,我想前面的詞,例如'16th'
我一直在嘗試使用LOCATE和REVERSE函數,但無法使其工作。誰能幫忙?
謝謝!
這應該是簡單的,但我在細節迷路....MySQL的定位和反向查詢
在數據庫領域,如果字符串包含一個字「世紀」,我想前面的詞,例如'16th'
我一直在嘗試使用LOCATE和REVERSE函數,但無法使其工作。誰能幫忙?
謝謝!
你可以嘗試:
使用SUBSTRING_INDEX之前 '世紀' 返回字符串。
反向使用反轉字符串。
使用substring_index在第一個空格之前返回字符串(可能需要修剪它?)。
反向使用以再次反轉字符串。
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)
謝謝!作爲參考,我最終使用的SQL是:concat(REVERSE(SUBSTRING_INDEX(REVERSE(TRIM(SUBSTRING_INDEX(1case(FREETEXT_DETAIL_EN),'century',1))),'',1)),'','century' ) – JezB