帶有 ORDER BY FIELD 的 MySQL IF/WHEN/ELSE/OR


我們先建立一個表 −

mysql> create table DemoTable1884
   (
   Marks int
   );
Query OK, 0 rows affected (0.00 sec)

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

mysql> insert into DemoTable1884 values(55);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1884 values(97);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1884 values(79);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1884 values(87);
Query OK, 1 row affected (0.00 sec)

使用 insert 命令顯示錶中的某些記錄 −

mysql> select * from DemoTable1884;

這將產生以下輸出 −

+-------+
| Marks |
+-------+
|    55 |
|    97 |
|    79 |
|    87 |
+-------+
4 rows in set (0.00 sec)

這是使用 OR 實現 order by field() 的查詢

mysql> select * from DemoTable1884 where Marks = 98 or Marks = 87 order by field(Marks,98,87) limit 1;

這將產生以下輸出 −

+-------+
| Marks |
+-------+
|    87 |
+-------+
1 row in set (0.00 sec)

更新於:2019 年 12 月 27 日

223 次觀看

啟動您的 職業

透過完成課程獲得認證

開始
廣告