在包含产品详细信息的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)
猜你喜欢