這是一個例子:
create table lastword (
id int not null auto_increment primary key,
mytext varchar(250)
) engine = myisam;
insert into lastword (mytext) values
('this is my car'),
('how are you,doing'),
('how is your)health'),
('this is not(working');
select *,
substring_index(replace(replace(replace(mytext,',',' '),')',' '),'(',' '),' ',-1) as last
from lastword
+----+---------------------+---------+
| id | mytext | last |
+----+---------------------+---------+
| 1 | this is my car | car |
| 2 | how are you,doing | doing |
| 3 | how is your)health | health |
| 4 | this is not(working | working |
+----+---------------------+---------+
4 rows in set (0.00 sec)
正如你可以看到你將不得不使用大量的嵌套內容替換來完成你的任務。
有substring_index()函數,但在你的情況下它不起作用,因爲你總是有不同的字符(空格,逗號,括號等)。我認爲你需要一些正則表達式,或者你必須使用大量的嵌套替換來刪除這些字符,然後再使用該函數。 – 2011-04-25 20:56:36
我認爲只要讀取每一行並使用RegEx(或其他)來獲取代碼中的最後一個詞就會更好。 http://stackoverflow.com/questions/4380692/find-the-position-of-a-regex-substring-in-mysql – Compeek 2011-04-25 20:59:22