執行 MySQL ORDER BY 關鍵字匹配?


為此,讓我們建立一個表,插入一些值並使用 ORDER BY CASE。讓我們首先建立一個表 −

mysql> create table DemoTable602 (GameName text);
Query OK, 0 rows affected (0.55 sec)

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

mysql> insert into DemoTable602 values('Candy cash game');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable602 values('Pubg');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable602 values('cash Candy game');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable602 values('subway');
Query OK, 1 row affected (0.14 sec)

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

mysql> select *from DemoTable602;

這將產生以下輸出 −

+-----------------+
| GameName        |
+-----------------+
| Candy cash game |
| Pubg            |
| cash Candy game |
| subway          |
+-----------------+
4 rows in set (0.00 sec)

以下是執行 MySQL ORDER BY 關鍵字匹配的查詢 −

mysql> select *from DemoTable602
   order by
      CASE WHEN instr(GameName, 'Candy') = 0 then 1 else 0 end,
      instr(GameName, 'cash') desc;

這將產生以下輸出 −

+-----------------+
| GameName        |
+-----------------+
| Candy cash game |
| cash Candy game |
| Pubg            |
| subway          |
+-----------------+
4 rows in set (0.00 sec)

更新於: 22-Aug-2019

115 次瀏覽

開啟你的 職業生涯

完成課程取得認證

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