如何在 MySQL 中仅显示总值中的 200 个字符?

mysqlmysqli database

您可以使用 MySQL 中的 LEFT() 显示 MySQL 中整个值中的某些字符。语法如下:

select left(yourColumnName ,200 ) AS anyAliasName from yourTableName;

首先我们创建一个表:

mysql> create table DemoTable (Paragraph longtext);
Query OK, 0 rows affected (0.71 sec)

以下是使用 insert 命令在表中插入记录的查询:

mysql> insert into DemoTable values('Introduction to Java,Introduction to C,Introduction to
C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to
MySQL,Introduction to MongoDB,Introduction to SQL Server,Introduction to
ASP.net,Introduction to JSF');
Query OK, 1 row affected (0.13 sec)

以下是使用 select 命令显示表中记录的查询:

mysql> select *from DemoTable;

这将产生以下输出:

+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Paragraph                                                                                                                                                                                                                                                               |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Server,Introduction to ASP.net,Introduction to JSF                |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

以下查询仅显示总值中的 200 个字符:

mysql> select left(Paragraph ,200) AS `200Characters` from DemoTable;

这将产生以下输出,仅显示前 200 个字符:

+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 200Characters                                                                                                                                                                                                                         |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Ser                                                                                                                                                                                                        |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

相关文章