如何从 MySQL 文本字段中检索前 40 个字符?
要从文本字段获取前 40 个字符,请使用 MySQL 中的 LEFT() 函数。语法如下 −
SELECT LEFT(yourColumnName,40) as anyVariableName from yourTableName;
为了了解上述概念,让我们创建一个表。创建表的查询如下 −
mysql> create table retrieveFirst40Characters −> ( −> AllWords text −> ); Query OK, 0 rows affected (0.59 sec)
现在,你可以借助插入命令将一些记录插入到表中。查询如下 −
mysql> insert into retrieveFirst40Characters values('This is a query demo to extract a forty characters from a text '> field,you can use left function to get forty characters'); Query OK, 1 row affected (0.32 sec)
使用 select 语句显示表中的所有记录。查询如下 −
mysql> select *from retrieveFirst40Characters;
以下是输出 −
+------------------------------------------------------------------------------------------------------------------------+ | AllWords | +------------------------------------------------------------------------------------------------------------------------+ | This is a demo text displayed in a table for our example | +------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
以下是如何从文本字段中提取前 40 个字符的查询 −
mysql> select left(AllWords,40) as Retrieve40Characters from retrieveFirst40Characters;
以下是显示前 40 个字符的输出 −
+------------------------------------------+ | Retrieve40Characters | +------------------------------------------+ | This is a demo text displayed in a table | +------------------------------------------+ 1 row in set (0.03 sec)
广告