MySQL 中是否有用于行而不是列的 MAX 函数?

mysqlmysqli database更新于 2025/9/3 23:08:17

是的,您可以使用 MySQL 中的 GREATEST() 检查行(而不是列)的最大值。让我们首先创建一个表 −

mysql> create table DemoTable
   (
   Value1 int,
   Value2 int,
   Value3 int
   );
Query OK, 0 rows affected (0.58 sec)

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

mysql> insert into DemoTable values(190,395,322);
Query OK, 1 row affected (0.16 sec)

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

mysql> select *from DemoTable;

这将产生以下输出 −

+--------+--------+--------+
| Value1 | Value2 | Value3 |
+--------+--------+--------+
| 190    | 395    | 322    |
+--------+--------+--------+
1 row in set (0.00 sec)

这是获取行(不是列)的最大值的查询 −

mysql> select greatest(Value1,Value2,Value3) as GreaterValue from DemoTable;

这将产生以下输出 −

+--------------+
| GreaterValue |
+--------------+
| 395          |
+--------------+
1 row in set (0.04 sec)

相关文章


有用资源