MySQL 如何计算一个字段所有行中的所有字符数

MySQL 如何计算一个字段所有行中的所有字符数

以下是计算一个字段中所有行中的所有字符数的语法 –

select sum(char_length(yourColumnName)) AS anyAliasName from yourTableName;

要了解上述语法,让我们创建一个表.

创建表的查询如下 –

mysql> create table CountAllCharactersDemo( 
-> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, 
-> UserName varchar(20), 
-> UserSubject text 
-> );

使用插入命令在表中插入一些记录. 查询如下 –

mysql> insert into CountAllCharactersDemo(UserName,UserSubject)
values('Larry','Introduction To Java');
Query OK, 1 row affected (0.19 sec)
mysql> insert into CountAllCharactersDemo(UserName,UserSubject)
values('Mike','Introduction To Computer Networks');
Query OK, 1 row affected (0.21 sec)
mysql> insert into CountAllCharactersDemo(UserName,UserSubject)
values('Sam','Introduction To C');
Query OK, 1 row affected (0.18 sec)
mysql> insert into CountAllCharactersDemo(UserName,UserSubject)
values('Carol','Introduction To Python');
Query OK, 1 row affected (0.25 sec)
mysql> insert into CountAllCharactersDemo(UserName,UserSubject)
values('David','Introduction To Spring And Hibernate Framework');
Query OK, 1 row affected (0.15 sec)

使用select语句从表中显示所有记录. 查询如下-

mysql> select *from CountAllCharactersDemo;

下面是结果 –

+--------+----------+------------------------------------------------+
| UserId | UserName | UserSubject                                    |
+--------+----------+------------------------------------------------+
| 1      | Larry    | Introduction To Java                           |
| 2      | Mike     | Introduction To Computer Networks              |
| 3      | Sam      | Introduction To C                              |
| 4      | Carol    | Introduction To Python                         |
| 5      | David    | Introduction To Spring And Hibernate Framework |
+--------+----------+------------------------------------------------+
5 rows in set (0.00 sec)

以下是计算MySQL中一个字段所有行中所有字符数的查询.

案例1 - 计算总长度.

查询如下 –

mysql> select sum(char_length(UserSubject)) AS AllCharactersLength from CountAllCharactersDemo;

下面是结果 –

+---------------------+
| AllCharactersLength |
+---------------------+
| 138                 |
+---------------------+
1 row in set (0.00 sec)

案例2 - 计算每行长度的查询 –

mysql> select UserId,UserName,UserSubject,char_length(UserSubject) AS Length from
CountAllCharactersDemo;

下面是输出结果 –

+--------+----------+------------------------------------------------+--------+
| UserId | UserName | UserSubject                                    | Length |
+--------+----------+------------------------------------------------+--------+
| 1      | Larry    | Introduction To Java                           | 20     |
| 2      | Mike     | Introduction To Computer Networks              | 33     |
| 3      | Sam      | Introduction To C                              | 17     |
| 4      | Carol    | Introduction To Python                         | 22     |
| 5      | David    | Introduction To Spring And Hibernate Framework | 46     |
+--------+----------+------------------------------------------------+--------+
5 rows in set (0.00 sec)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程