这应该是简单的,但我在细节迷路....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