简体   繁体   English

MySQL定位和反向查询

[英]MySQL Locate and Reverse query

This should be simple, but I'm getting lost in the detail.... 这应该很简单,但是我迷失了细节。

In a database field, if the string contains a the word 'century', I would like the previous word, eg '16th' 在数据库字段中,如果字符串中包含单词“ century”,我希望使用之前的单词,例如“ 16th”

I've been trying using the LOCATE and REVERSE functions, but cant make it work. 我一直在尝试使用LOCATE和REVERSE函数,但无法使其正常工作。 Can anyone help? 有人可以帮忙吗?

Thank you! 谢谢!

You could try: 您可以尝试:

Use substring_index to return the string before 'century'. 使用substring_index返回'century'之前的字符串。
Use reverse to reverse the string. 使用reverse反转字符串。
Use substring_index to return the string before the first space (might need to trim it?). 使用substring_index在第一个空格之前返回字符串(可能需要修整吗?)。
Use reverse to reverse the string again. 使用反向将字符串再次反向。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM