MySQL查询从重复的列值中获取最大对应值
让我们首先创建一个表-
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)
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短