获取 MySQL 中所选行的长度
要获取所选行的长度,请使用 CHAR_LENGTH()。我们先创建一个表 −
mysql> create table DemoTable1612 -> ( -> FirstName varchar(20), -> LastName varchar(20) -> ); Query OK, 0 rows affected (0.87 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1612 values('David','Brown'); Query OK, 1 row affected (0.75 sec) mysql> insert into DemoTable1612 values('John','Smith'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1612 values('Bob','Taylor'); Query OK, 1 row affected (0.13 sec)
使用 select 语句从表中显示所有记录 −
mysql> select * from DemoTable1612;
这将产生以下输出 −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | David | Brown | | John | Smith | | Bob | Taylor | +-----------+----------+ 3 rows in set (0.00 sec)
以下是获取 MySQL 中所选行长度的查询 −
mysql> select sum(char_length(FirstName)) from DemoTable1612 where LastName IN('Brown','Smith','Taylor');
这将产生以下输出 −
+-----------------------------+ | sum(char_length(FirstName)) | +-----------------------------+ | 12 | +-----------------------------+ 1 row in set (0.00 sec)
广告