如何選擇一個欄位與存在 MAX() 的欄位對應?
為此,您可以將子查詢與聚合函式 MAX() 結合使用。我們首先建立一個表 -
mysql> create table DemoTable -> ( -> ProductId int, -> ProductAmount int -> ); Query OK, 0 rows affected (0.78 sec)
使用 insert 命令在表中插入一些記錄 -
mysql> insert into DemoTable values(1001,7895); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(1003,8903); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(1010,7690); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(2010,8450); Query OK, 1 row affected (0.12 sec)
使用 select 語句顯示錶中的所有記錄 -
mysql> select *from DemoTable;
這將產生以下輸出 -
+-----------+---------------+ | ProductId | ProductAmount | +-----------+---------------+ | 1001 | 7895 | | 1003 | 8903 | | 1010 | 7690 | | 2010 | 8450 | +-----------+---------------+ 4 rows in set (0.00 sec)
以下是查詢選擇對應於 max() 中欄位的欄位 -
mysql> select *from DemoTable -> where ProductAmount=( select max(ProductAmount) from DemoTable);
這將產生以下輸出 -
+-----------+---------------+ | ProductId | ProductAmount | +-----------+---------------+ | 1003 | 8903 | +-----------+---------------+ 1 row in set (0.05 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言
C++
C#
MongoDB
MySQL
Javascript
PHP