您可以借助 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 中按字符長度對字符串進行排序?的詳細內容,更多請關注www.92cms.cn其它相關文章!