MySQL ORDER BY 含有下劃線的字串?


讓我們首先建立一個表 -

mysql> create table DemoTable
(
   Name varchar(100)
);
Query OK, 0 rows affected (0.60 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable values('John_Smith');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Chris Brown');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('John_Doe');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('David Miller');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('Carol Taylor');
Query OK, 1 row affected (0.13 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+--------------+
| Name         |
+--------------+
| John_Smith   |
| Chris Brown  |
| John_Doe     |
| David Miller |
| Carol Taylor |
+--------------+
5 rows in set (0.00 sec)

以下是按帶有下劃線的字串排序的查詢 -

mysql> select *from DemoTable order by (Name LIKE 'John_%') DESC,rand();

這將產生以下輸出 -

+--------------+
| Name         |
+--------------+
| John_Doe     |
| John_Smith   |
| Carol Taylor |
| David Miller |
| Chris Brown  |
+--------------+
5 rows in set (0.42 sec)

更新於: 2019-09-26

259 次瀏覽

開啟您的 職業生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.