在 MySQL 中从单列中获取最高分数值以及从两列中获取最大分数值

mysqlmysqli database更新于 2025/11/14 23:40:17

首先我们创建一个表 −

mysql> create table DemoTable790 (
   Score1 int,
   Score2 int
);
Query OK, 0 rows affected (0.45 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable790 values(98,76);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable790 values(78,89);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable790 values(85,68);
Query OK, 1 row affected (0.14 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable790;

这将产生以下输出 -

+--------+--------+
| Score1 | Score2 |
+--------+--------+
| 98     | 76     |
| 78     | 89     |
| 85     | 68     |
+--------+--------+
3 rows in set (0.00 sec)

max() 如下,用于从单个列中获取最大值 −

mysql> select max(Score1) from DemoTable790;

这将产生以下输出 -

+-------------+
| max(Score1) |
+-------------+
| 98          |
+-------------+
1 row in set (0.00 sec)

greater() 如下,用于从两列中获取最大值 −

mysql> select greatest(Score1,Score2) from DemoTable790;

这将产生以下输出 -

+-------------------------+
| greatest(Score1,Score2) |
+-------------------------+
| 98                      |
| 89                      |
| 85                      |
+-------------------------+
3 rows in set (0.03 sec)

相关文章


有用资源