为此,请使用聚合函数MIN()和GROUP BY。在这里,我们将显示NumberOfProduct的最小ID。让我们首先创建一个表-
create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, NumberOfProduct int );
使用插入命令在表中插入一些记录-
insert into DemoTable(NumberOfProduct) values(40); insert into DemoTable(NumberOfProduct) values(40); insert into DemoTable(NumberOfProduct) values(60); insert into DemoTable(NumberOfProduct) values(60);
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+----+-----------------+ | Id | NumberOfProduct | +----+-----------------+ | 1 | 40 | | 2 | 40 | | 3 | 60 | | 4 | 60 | +----+-----------------+ 4 rows in set (0.00 sec)
以下是查询以拉出列中编号最小的行-
select NumberOfProduct,MIN(Id) from DemoTable group by NumberOfProduct;
这将产生以下输出-
+-----------------+---------+ | NumberOfProduct | MIN(Id) | +-----------------+---------+ | 40 | 1 | | 60 | 3 | +-----------------+---------+ 2 rows in set (0.00 sec)