要从具有相应重复ID的列中获取最小值,请使用GROUP BY和MIN()
-
select min(yourColumnName) from yourTableName group by yourColumnName;
要了解上述语法,让我们创建一个表-
mysql> create table DemoTable2005 ( Id int, Price float );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable2005 values(1,56.88); mysql> insert into DemoTable2005 values(1,120.56);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable2005;
这将产生以下输出-
+------+--------+ | Id | Price | +------+--------+ | 1 | 56.88 | | 1 | 120.56 | +------+--------+ 2 rows in set (0.00 sec)
这是从一列中获取最小值的查询,其中对应的列具有重复的ID-
mysql> select min(Price) from DemoTable2005 group by Id;
这将产生以下输出-
+------------+ | min(Price) | +------------+ | 56.88 | +------------+ 1 row in set (0.00 sec)