让我们首先创建一个表-
mysql> create table DemoTable( ProductName varchar(100), ProductPrice int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Product-1',56); mysql> insert into DemoTable values('Product-2',78); mysql> insert into DemoTable values('Product-1',88); mysql> insert into DemoTable values('Product-2',86); mysql> insert into DemoTable values('Product-1',45); mysql> insert into DemoTable values('Product-3',90); mysql> insert into DemoTable values('Product-2',102); mysql> insert into DemoTable values('Product-3',59);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------------+--------------+ | ProductName | ProductPrice | +-------------+--------------+ | Product-1 | 56 | | Product-2 | 78 | | Product-1 | 88 | | Product-2 | 86 | | Product-1 | 45 | | Product-3 | 90 | | Product-2 | 102 | | Product-3 | 59 | +-------------+--------------+ 8 rows in set (0.00 sec)
以下是从重复的列值中获取最大对应值的查询。在这里,我们找到了重复列(例如Product-1,Product-2等)的最大ProductPrice-
mysql> select ProductName,MAX(ProductPrice) from DemoTable group by ProductName;
这将产生以下输出-
+-------------+-------------------+ | ProductName | MAX(ProductPrice) | +-------------+-------------------+ | Product-1 | 88 | | Product-2 | 102 | | Product-3 | 90 | +-------------+-------------------+ 3 rows in set (0.00 sec)