MySQL查询获取多个最小值?

为此,您可以将子查询与一起使用MIN()。让我们首先创建一个表-

mysql> create table DemoTable
   -> (
   -> Name varchar(20),
   -> Score int
   -> );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable values('John',56);
mysql> insert into DemoTable values('John',45);
mysql> insert into DemoTable values('John',58);
mysql> insert into DemoTable values('Chris',43);
mysql> insert into DemoTable values('Chris',38);
mysql> insert into DemoTable values('Chris',87);

使用select语句显示表中的所有记录-

mysql> select *from DemoTable;
+-------+-------+
| Name  | Score |
+-------+-------+
| John  |    56 |
| John  |    45 |
| John  |    58 |
| Chris |    43 |
| Chris |    38 |
| Chris |    87 |
+-------+-------+
6 rows in set (0.00 sec)

这是获取多个最小值的查询-

mysql> select *from DemoTable tbl1
   -> where Score IN( select min(Score) from DemoTable tbl2 where tbl1.Name=tbl2.Name);
+-------+-------+
| Name  | Score |
+-------+-------+
| John  |    45 |
| Chris |    38 |
+-------+-------+
2 rows in set (0.00 sec)