Greatest value of multiple columns with column name?(具有列名的多列的最大值?)
问题描述
关于如何从多列中返回最大值以及列名,我已经探索了许多问题和答案.但我还没有解决问题这里在这个问题中将解释:
I have explored many questions and answers regarding how to return the greatest values from multiple columns along with column name. But I have not solved the problem here in this question will explain:
多列的SQL MAX?
但我不知道如何找出具有最大价值的列名?有什么想法吗?
But I don't know how to figure out the column name having greatest value? Any ideas?
推荐答案
您可以使用 GREATEST 函数在所有列中找到最大值:
You can use GREATEST function to find the maximum value among all columns:
SELECT @var_max_val:= GREATEST(col1, col2, col3, ...) AS max_value,
CASE @var_max_val WHEN col1 THEN 'col1'
WHEN col2 THEN 'col2'
...
END AS max_value_column_name
FROM table_name
WHERE ...
这篇关于具有列名的多列的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有列名的多列的最大值?
基础教程推荐
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在多列上分布任意行 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
