MySQL在两个段落标记之间获取文本子字符串

时间:2014-04-19 19:42:46

标签: mysql substring

我需要获得行条目的第一段。我怎么能通过查询来做到这一点

1 个答案:

答案 0 :(得分:0)

以下是使用SUBSTRING_INDEX()函数返回文本块的第一段的演示,假设段落由换行符分隔。

mysql> create table t (t text);

mysql> insert into t (t) values ('now is the time\nfor all good men');

mysql> select * from t;
+----------------------------------+
| t                                |
+----------------------------------+
| now is the time
for all good men |
+----------------------------------+

mysql> select substring_index(t, '\n', 1) from t;
+-----------------------------+
| substring_index(t, '\n', 1) |
+-----------------------------+
| now is the time             |
+-----------------------------+