MySQL从一组列中查找最大计数

标签 mysql sql

目前我有 9 列

Brand, ID, M, T, W, Th, Fri, Sat, Sun
abc, 1, 2, 4, 6, 3, 7, 1,  5

我想找出以下哪组的个体数最多。 月/周/周/日、周五、周六/周日。例如,在上表中,将选择星期五,因为它的计数最高。

最佳答案

假设值之间没有联系,您可以使用 greatestcase 表达式来完成此操作。如果存在平局,则将返回 case 表达式中遇到的第一个值(平局天数中的值)。

select case greatest(M, T, W, Th, Fri, Sat, Sun) 
            when M then 'Monday'
            when T then 'Tuesday'
            when W then 'Wednesday'
            when Th then 'Thursday'
            when Fri then 'Friday'
            when Sat then 'Saturday'
            when Sun then 'Sunday'
        end 
from tbl

关于MySQL从一组列中查找最大计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46409456/

相关文章:

php - 如何使用mysql显示下一个日期

mysql - SQL 连接 with where 和having count() 条件

php - 如何使用 php 和 mysql 排列数字而不重复

mysql - 无法从 Emacs shell 缓冲区运行 mysql

php - 如何更新mysql数据库中存储的数据

sql - 防止在 Postgres 中为特定查询使用索引

sql - Postgres : "cumulative" view of table

mysql - 如何使用etl工具将excel表转换成mysql数据库

mysql - 基于多条件的SQL查询

mysql - 票务系统 : sql tables