在本教學中,您將學習如何使用MySQL MIN()
函式來查詢一組值中的最小值。
MIN()
函式返回一組值中的最小值。MIN()
函式在某些情況下非常有用,例如找到最小的數位,選擇最便宜的產品,獲得最低的信用額度等。
以下說明MIN()
函式的語法:
MIN(DISTINCT expression);
如果指定DISTINCT
運算子,則MIN
函式返回不同值的最小值,與省略DISTINCT
相同。換句話說,DISTINCT
運算子對MIN
函式沒有任何影響,它只是為了ISO
相容性。
請注意,DISTINCT
運算子在其他聚合函式(如SUM,AVG和COUNT)中生效。
我們來看看範例資料庫(yiibaidb)中的products
表,其結構如下表所示 -
mysql> desc products;
+--------------------+---------------+------+-----+---------+------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+------------------+
| productCode | varchar(15) | NO | PRI | | |
| productName | varchar(70) | NO | MUL | NULL | |
| productLine | varchar(50) | NO | MUL | NULL | |
| productScale | varchar(10) | NO | | NULL | |
| productVendor | varchar(50) | NO | | NULL | |
| productDescription | text | NO | | NULL | |
| quantityInStock | smallint(6) | NO | | NULL | |
| buyPrice | decimal(10,2) | NO | | NULL | |
| MSRP | decimal(10,2) | NO | | NULL | |
| stockValue | double | YES | | NULL | STORED GENERATED |
+--------------------+---------------+------+-----+---------+------------------+
10 rows in set
要查詢獲得products
表中最便宜的產品,請使用以下查詢:
SELECT
MIN(buyPrice)
FROM
products;
執行上面查詢語句,得到以下結果 -
mysql> SELECT
MIN(buyPrice)
FROM
products;
+---------------+
| MIN(buyPrice) |
+---------------+
| 15.91 |
+---------------+
1 row in set
要不僅選擇價格,還要查詢產品程式碼和產品名稱等其他產品資訊,可以在子查詢中使用MIN
函式,如下所示:
SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice = (
SELECT
MIN(buyPrice)
FROM
products);
執行上面查詢語句,得到以下結果 -
+-------------+-------------------------------------+----------+
| productCode | productName | buyPrice |
+-------------+-------------------------------------+----------+
| S24_2840 | 1958 Chevy Corvette Limited Edition | 15.91 |
+-------------+-------------------------------------+----------+
1 row in set
上面語句怎麼執行?
products
表中最低的價格產品。當您將MIN
函式與SELECT語句中的GROUP BY子句相結合使用時,可以獲取每個組的最小值。
例如,要查詢每個產品線的最低價格產品,請使用以下語句:
SELECT
productline, MIN(buyprice)
FROM
products
GROUP BY productline;
執行上面查詢語句,得到以下結果 -
mysql> SELECT
productline, MIN(buyprice)
FROM
products
GROUP BY productline;
+------------------+---------------+
| productline | MIN(buyprice) |
+------------------+---------------+
| Classic Cars | 15.91 |
| Motorcycles | 24.14 |
| Planes | 29.34 |
| Ships | 33.3 |
| Trains | 26.72 |
| Trucks and Buses | 24.92 |
| Vintage Cars | 20.61 |
+------------------+---------------+
7 rows in set
如果您不僅要選擇產品線,還要查詢products
表中的其他列,例如產品程式碼和產品名稱,則需要使用相關的子查詢。
以下查詢通過將MIN()
函式與相關子查詢相結合來查詢每個產品線中的最低價格產品:
SELECT
productline, productCode, productName, buyprice
FROM
products a
WHERE
buyprice = (
SELECT
MIN(buyprice)
FROM
products b
WHERE
b.productline = a.productline);
執行上面查詢語句,得到以下結果 -
+------------------+-------------+-------------------------------------------+----------+
| productline | productCode | productName | buyprice |
+------------------+-------------+-------------------------------------------+----------+
| Trucks and Buses | S18_2432 | 1926 Ford Fire Engine | 24.92 |
| Vintage Cars | S24_2022 | 1938 Cadillac V-16 Presidential Limousine | 20.61 |
| Classic Cars | S24_2840 | 1958 Chevy Corvette Limited Edition | 15.91 |
| Planes | S24_3949 | Corsair F4U ( Bird Cage) | 29.34 |
| Motorcycles | S32_2206 | 1982 Ducati 996 R | 24.14 |
| Trains | S32_3207 | 1950s Chicago Surface Lines Streetcar | 26.72 |
| Ships | S72_3212 | Pont Yacht | 33.3 |
+------------------+-------------+-------------------------------------------+----------+
7 rows in set
對於來自外部查詢的每個產品線,相關子查詢選擇產品線中的最低價格產品並返回最低價格。 然後將返回的最低價格用作外部查詢的輸入,以選擇相關產品資料,包括產品線,產品程式碼,產品名稱和價格。
如果要在不使用MIN
函式和子查詢的情況下實現相同的結果,則可以使用帶有LEFT JOIN
子句的自聯接,如下查詢語句:
SELECT
a.productline, a.productCode, a.productName, a.buyprice
FROM
products a
LEFT JOIN
products b ON a.productline = b.productline
AND b.buyprice < a.buyprice
WHERE
b.productcode IS NULL;
執行上面查詢語句,得到以下結果 -
+------------------+-------------+-------------------------------------------+----------+
| productline | productCode | productName | buyprice |
+------------------+-------------+-------------------------------------------+----------+
| Trucks and Buses | S18_2432 | 1926 Ford Fire Engine | 24.92 |
| Vintage Cars | S24_2022 | 1938 Cadillac V-16 Presidential Limousine | 20.61 |
| Classic Cars | S24_2840 | 1958 Chevy Corvette Limited Edition | 15.91 |
| Planes | S24_3949 | Corsair F4U ( Bird Cage) | 29.34 |
| Motorcycles | S32_2206 | 1982 Ducati 996 R | 24.14 |
| Trains | S32_3207 | 1950's Chicago Surface Lines Streetcar | 26.72 |
| Ships | S72_3212 | Pont Yacht | 33.3 |
+------------------+-------------+-------------------------------------------+----------+
7 rows in set
在本教學中,您已經學習了如何使用MySQL MIN
函式來查詢一組值中的最小值。