您可以使用 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中文網其他相關文章!