MySQL 查詢可從具有多列的單行中獲取最高值\n\n\n\n\n\n\n\n\n\n


要獲取最高值,請使用 GREATEST() 方法。我們首先建立一個表 −

mysql> create table DemoTable
   -> (
   -> Value1 int,
   -> Value2 int,
   -> Value3 int
   -> );
Query OK, 0 rows affected (1.29 sec)

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

mysql> insert into DemoTable values(100,600,400);
Query OK, 1 row affected (0.19 sec)

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

mysql> select *from DemoTable;

輸出

+--------+--------+--------+
| Value1 | Value2 | Value3 |
+--------+--------+--------+
| 100    | 600    | 400    |
+--------+--------+--------+
1 row in set (0.00 sec)

以下是用於獲取最高值的查詢 −

mysql> select greatest(Value1,Value2,Value3) AS HighestFrom1Row from DemoTable;

輸出

+-----------------+
| HighestFrom1Row |
+-----------------+
|   600           |
+-----------------+
1 row in set (0.00 sec)

更新於: 2019 年 7 月 30 日

155 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告