从 3 个不同的列中获取 MySQL 最大值?
要从三列中获取最大值,请使用 GREATEST() 函数。
语法如下
SELECT GREATEST(yourColumnName1,yourColumnName2,yourColumnName3) AS anyAliasName FROM yourTableName;
为了理解以上语法,让我们创建一个表。创建表的查询如下
mysql> create table MaxOfThreeColumnsDemo -> ( -> First int, -> Second int, -> Third int -> ); Query OK, 0 rows affected (0.73 sec)
使用插入命令在表中插入一些记录。
查询如下
mysql> insert into MaxOfThreeColumnsDemo values(30,90,60); Query OK, 1 row affected (0.16 sec) mysql> insert into MaxOfThreeColumnsDemo values(100,40,50); Query OK, 1 row affected (0.20 sec) mysql> insert into MaxOfThreeColumnsDemo values(101,290,150); Query OK, 1 row affected (0.22 sec)
使用 select 语句从表中显示所有记录。
查询如下
mysql> select *from MaxOfThreeColumnsDemo;
以下是输出
+-------+--------+-------+ | First | Second | Third | +-------+--------+-------+ | 30 | 90 | 60 | | 100 | 40 | 50 | | 101 | 290 | 150 | +-------+--------+-------+ 3 rows in set (0.00 sec)
以下是查找三个列中最大值点的查询
mysql> select greatest(First,Second,Third) AS MAXValueOfThreeColumns from MaxOfThreeColumnsDemo;
以下是输出
+------------------------+ | MAXValueOfThreeColumns | +------------------------+ | 90 | | 100 | | 290 | +------------------------+ 3 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP