如何統計 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 -> ); Query OK, 0 rows affected (0.47 sec)
使用 insert 命令插入一些記錄到表中。查詢如下 −
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)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP