您可以使用聚合函数MAX()
,并MIN()
为这个。
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Number1 int, Number2 int );
使用插入命令在表中插入记录-
mysql> insert into DemoTable(Number1,Number2) values(67,45); mysql> insert into DemoTable(Number1,Number2) values(90,40); mysql> insert into DemoTable(Number1,Number2) values(80,43);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable ;
这将产生以下输出-
+----+---------+---------+ | Id | Number1 | Number2 | +----+---------+---------+ | 1 | 67 | 45 | | 2 | 90 | 40 | | 3 | 80 | 43 | +----+---------+---------+ 3 rows in set (0.00 sec)
以下是获取MAX和MIN值及其行ID的查询-
mysql> select * from DemoTable where Number1 = (select max(Number1) from DemoTable) or Number2 = (select min(Number2) from DemoTable);
这将产生以下输出-
+----+---------+---------+ | Id | Number1 | Number2 | +----+---------+---------+ | 2 | 90 | 40 | +----+---------+---------+ 1 row in set (0.00 sec)