忽略MySQL中特定單詞的列排序


我們先來建立一個表 -

mysql> create table DemoTable
   -> (
   -> Name text
   -> );
Query OK, 0 rows affected (1.31 sec)

使用 insert 命令在表中插入一些記錄。在這裡,我們插入了一個包含特定單詞“name”的名稱,我們需要忽略它 -

mysql> insert into DemoTable values('John 7');
Query OK, 1 row affected (0.65 sec)

mysql> insert into DemoTable values('John 6');
Query OK, 1 row affected (0.42 sec)

mysql> insert into DemoTable values('John 9');
Query OK, 1 row affected (0.33 sec)

mysql> insert into DemoTable values('name John 3');
Query OK, 1 row affected (0.24 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

輸出

這將產生以下輸出 -

+-------------+
| Name        |
+-------------+
| John 7      |
| John 6      |
| John 9      |
| name John 3 |
+-------------+
4 rows in set (0.00 sec)

以下是忽略特定單詞對列進行排序的查詢 -

mysql> select *from DemoTable ORDER BY TRIM(REPLACE(LOWER(Name), 'name ', ''));

輸出

這將產生以下輸出 -

+-------------+
| Name        |
+-------------+
| name John 3 |
| John 6      |
| John 7      |
| John 9      |
+-------------+
4 rows in set (0.00 sec)

更新於:2020-06-30

154 瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.