让我们首先创建一个表-
mysql> create table DemoTable1875 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Class varchar(20), Amount int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1875(Class,Amount) values('X',750); mysql> insert into DemoTable1875(Class,Amount) values('X',140); mysql> insert into DemoTable1875(Class,Amount) values('X',450); mysql> insert into DemoTable1875(Class,Amount) values('Y',6780); mysql> insert into DemoTable1875(Class,Amount) values('Z',1350); mysql> insert into DemoTable1875(Class,Amount) values('Z',2050);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1875;
这将产生以下输出-
+----+-------+--------+ | Id | Class | Amount | +----+-------+--------+ | 1 | X | 750 | | 2 | X | 140 | | 3 | X | 450 | | 4 | Y | 6780 | | 5 | Z | 1350 | | 6 | Z | 2050 | +----+-------+--------+ 6 rows in set (0.00 sec)
以下是查询以选择行
mysql> select Class,Min(Amount) from DemoTable1875 group by Class;
这将产生以下输出-
+-------+-------------+ | Class | Min(Amount) | +-------+-------------+ | X | 140 | | Y | 6780 | | Z | 1350 | +-------+-------------+ 3 rows in set (0.00 sec)