您可以借助 MySQL 的 CHAR_LENGTH() 函数按字符长度排序。该函数返回字符数,即以下字符串的 4 个 -
AMIT
要按字符长度对字符串进行排序,语法如下 -
select *from yourTableName order by CHAR_LENGTH(yourColumnName);
为了理解上面的概念,让我们首先创建一个表。以下是创建表的查询 -
mysql> create table OrderByCharacterLength −> ( −> BookName varchar(200) −> ); Query OK, 0 rows affected (1.97 sec)
借助insert命令在表中插入一些记录。查询如下 -
mysql> insert into OrderByCharacterLength values('Let us C'); Query OK, 1 row affected (0.31 sec) mysql> insert into OrderByCharacterLength values('Introduction to C'); Query OK, 1 row affected (0.20 sec) mysql> insert into OrderByCharacterLength values('Data Structure And Algorithm in Java '); Query OK, 1 row affected (0.13 sec) mysql> insert into OrderByCharacterLength values('C in Depth'); Query OK, 1 row affected (0.17 sec) mysql> insert into OrderByCharacterLength values('Java Projects'); Query OK, 1 row affected (0.23 sec)
让我们按上表中插入的顺序显示所有记录。查询如下 -
mysql> select *from OrderByCharacterLength;
以下是输出 -
+---------------------------------------+ | BookName | +---------------------------------------+ | Let us C | | Introduction to C | | Data Structure And Algorithm in Java | | C in Depth | | Java Projects | +---------------------------------------+ 5 rows in set (0.00 sec)
这里是显示按照字符长度排列的所有记录的查询。如果列值具有最小长度,那么它将获得更高的优先级并首先显示。
查询如下 -
mysql> select *from OrderByCharacterLength order by CHAR_LENGTH(BookName);
以下是输出 -
+---------------------------------------+ | BookName | +---------------------------------------+ | Let us C | | C in Depth | | Java Projects | | Introduction to C | | Data Structure And Algorithm in Java | +---------------------------------------+ 5 rows in set (0.00 sec)
以上是在 mYsql 中按字符长度对字符串进行排序?的详细内容。更多信息请关注PHP中文网其他相关文章!