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)
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练