MySQL 中有没有针对行而不是针对列的 MAX 函数?


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

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

使用插入命令将记录插入到表中 -

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)

更新时间: 30-Jul-2019

106 次浏览

启动您的职业生涯

完成该课程以获得认证

开始
广告
© . All rights reserved.