对于MySQL列中的最小值,请使用MIN()
方法;对于最大值,请使用MAX()
方法。让我们首先创建一个表-
mysql> create table DemoTable
(
CustomerName varchar(20),
ProductAmount int
) ;
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Chris',3599);
mysql> insert into DemoTable values('David',7843);
mysql> insert into DemoTable values('Mike',97474);
mysql> insert into DemoTable values('Bob',65884);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------------+---------------+
| CustomerName | ProductAmount |
+--------------+---------------+
| Chris | 3599 |
| David | 7843 |
| Mike | 97474 |
| Bob | 65884 |
+--------------+---------------+
4 rows in set (0.00 sec)
以下是使用MAX()
和获取最低和最高值的查询MIN()
-
mysql> select *from DemoTable where ProductAmount=(select max(ProductAmount) from DemoTable)
or ProductAmount=(select min(ProductAmount) from DemoTable);
这将产生以下输出-
+--------------+---------------+
| CustomerName | ProductAmount |
+--------------+---------------+
| Chris | 3599 |
| Mike | 97474 |
+--------------+---------------+
2 rows in set (0.00 sec)