在包含产品详细信息的MySQL表中显示金额最高的所有产品?
使用MAX()与子查询一起为这个在这里,MAX()用来获得的最大金额。让我们首先创建一个表-
mysql> create table DemoTable ( ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ProductName varchar(100), ProductAmount int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-1',60);
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-2',40);
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-3',75);
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-4',50);
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-5',75);使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+-------------+---------------+ | ProductId | ProductName | ProductAmount | +-----------+-------------+---------------+ | 1 | Product-1 | 60 | | 2 | Product-2 | 40 | | 3 | Product-3 | 75 | | 4 | Product-4 | 50 | | 5 | Product-5 | 75 | +-----------+-------------+---------------+ 5 rows in set (0.00 sec)
以下是查询以显示具有最大数量的所有产品-
mysql> select *from DemoTable where ProductAmount=(select max(ProductAmount) from DemoTable);
这将产生以下输出-
+-----------+-------------+---------------+ | ProductId | ProductName | ProductAmount | +-----------+-------------+---------------+ | 3 | Product-3 | 75 | | 5 | Product-5 | 75 | +-----------+-------------+---------------+ 2 rows in set (0.00 sec)
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便