SQL 最大()
MAX() 函數
MAX() 函數傳回指定列的最大值。
SQL MAX() 語法
SELECT MAX(column_name) FROM table_name;
示範資料庫
在本教學中,我們將使用php 樣本資料庫。
下面是選自"Websites" 表的資料:
+----+--------------+--- ------------------------+-------+---------+
| id | name | url | alexa | country |
+----+----------+------------------------------------ --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA | 1 |
| 2 | 淘寶 | https://www.taobao.com/ | 13 | CN cn/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 ://www.facebook.com/ | 3 | USA |
| 6 | 百度| https://www.baidu.com/ | 4 | CN |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---- -----------+-----------------------------------------+-------+-- -------+
| id | name | url | alexa | country |
+----+----------+------------------------------------ --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA | 1 |
| 2 | 淘寶 | https://www.taobao.com/ | 13 | CN cn/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 ://www.facebook.com/ | 3 | USA |
| 6 | 百度| https://www.baidu.com/ | 4 | CN |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---- -----------+-----------------------------------------+-------+-- -------+
下面的SQL 語句從"Websites" 表的"alexa" 欄位取得最大值:
範例SELECT MAX(alexa) AS max_alexa FROM Websites;
執行上述 SQL 結果如下所示: