要获得最长的varchar长度,您需要使用CHAR_LENGTH()。
语法如下
SELECT Max(CHAR_LENGTH(yourColumnName)) AS anyAliasName FROM yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下
mysql> create table CharLengthDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Sentence varchar(255) - > );
使用insert命令在表中插入一些记录。
查询如下
mysql> insert into CharLengthDemo(Sentence) values('Java is an object oriented programming language' - > ); mysql> insert into CharLengthDemo(Sentence) values('MySQL is a Relational Database Management System'); mysql> insert into CharLengthDemo(Sentence) values('JSP uses Java Programming Language'); mysql> insert into CharLengthDemo(Sentence) values('Object is a root class in java');
使用select语句显示表中的所有记录。
查询如下
mysql> select *from CharLengthDemo;
以下是输出
+----+--------------------------------------------------+ | Id | Sentence | +----+--------------------------------------------------+ | 1 | Java is an object oriented programming language | | 2 | MySQL is a Relational Database Management System | | 3 | JSP uses Java Programming Language | | 4 | Object is a root class in java | +----+--------------------------------------------------+ 4 rows in set (0.00 sec)
这是获取最长varchar长度的查询
mysql> SELECT Max(CHAR_LENGTH(Sentence)) AS MAXLENGTH FROM CharLengthDemo;
以下是显示具有最长varchar长度的值的输出
+-----------+ | MAXLENGTH | +-----------+ | 48 | +-----------+ 1 row in set (0.00 sec)