mysql函数min()与Max()的例子 使用Min()与Max()函数求最大、最小价格

发布时间:2020-03-21编辑:脚本学堂
本文介绍下,mysql中使用Min()与Max()函数,求最大小与最小价格的例子,学习下mysql的Min()与Max()函数的用法,有需要的朋友参考下。

mysql/function/ target=_blank class=infotextkey>mysql函数min()与Max()的实例代码。
例子:
 

复制代码 代码示例:

mysql> CREATE TABLE IF NOT EXISTS multimeters
    -> (
    ->   id             INT             AUTO_INCREMENT PRIMARY KEY,
    ->   model          CHAR(10)        NOT NULL,
    ->   price          DECIMAL(3,2)    NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO multimeters (model, price)   VALUES ("Standard", 11.75);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> INSERT INTO multimeters (model, price)   VALUES ("Super", 19.50);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> INSERT INTO multimeters (model, price)   VALUES ("DeLuxe", 24.99);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> SELECT * FROM multimeters;
+----+----------+-------+
| id | model    | price |
+----+----------+-------+
|  1 | Standard |  9.99 |
|  2 | Super    |  9.99 |
|  3 | DeLuxe   |  9.99 |
+----+----------+-------+
3 rows in set (0.00 sec)

mysql> SELECT MAX(price) AS max_price, MIN(price) AS min_price
    -> FROM multimeters; --//使用Min()、Max()求最小与最大价格
+-----------+-----------+
| max_price | min_price |
+-----------+-----------+
|      9.99 |      9.99 |
+-----------+-----------+
1 row in set (0.00 sec)

mysql> # delete this sample table
mysql> DROP TABLE IF EXISTS multimeters;
Query OK, 0 rows affected (0.00 sec)