您可以借助 MySQL 的 CHAR_LENGTH() 函數(shù)按字符長(zhǎng)度排序。該函數(shù)返回字符數(shù),即以下字符串的 4 個(gè) –
AMIT
登錄后復(fù)制
要按字符長(zhǎng)度對(duì)字符串進(jìn)行排序,語(yǔ)法如下 –
select *from yourTableName order by CHAR_LENGTH(yourColumnName);
登錄后復(fù)制
為了理解上面的概念,讓我們首先創(chuàng)建一個(gè)表。以下是創(chuàng)建表的查詢(xún) –
mysql> create table OrderByCharacterLength −> ( −> BookName varchar(200) −> ); Query OK, 0 rows affected (1.97 sec)
登錄后復(fù)制
借助insert命令在表中插入一些記錄。查詢(xún)?nèi)缦?–
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)
登錄后復(fù)制
讓我們按上表中插入的順序顯示所有記錄。查詢(xún)?nèi)缦?–
mysql> select *from OrderByCharacterLength;
登錄后復(fù)制
以下是輸出 –
+---------------------------------------+ | 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)
登錄后復(fù)制
這里是顯示按照字符長(zhǎng)度排列的所有記錄的查詢(xún)。如果列值具有最小長(zhǎng)度,那么它將獲得更高的優(yōu)先級(jí)并首先顯示。
查詢(xún)?nèi)缦?–
mysql> select *from OrderByCharacterLength order by CHAR_LENGTH(BookName);
登錄后復(fù)制
以下是輸出 –
+---------------------------------------+ | 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)
登錄后復(fù)制
以上就是在 mYsql 中按字符長(zhǎng)度對(duì)字符串進(jìn)行排序?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!