Mysql 在NULL开始出现在列中之前查找最后一级
我一篇文章最多有10层。我想从结构如下图所示的表中找到文章的最高级别。 预期产出为: 例如,对于文章,最高级别为F5。 另外,如果从F2到F10有相同的值,我们应该忽略该记录 请帮忙 问候,,Mysql 在NULL开始出现在列中之前查找最后一级,mysql,sql,Mysql,Sql,我一篇文章最多有10层。我想从结构如下图所示的表中找到文章的最高级别。 预期产出为: 例如,对于文章,最高级别为F5。 另外,如果从F2到F10有相同的值,我们应该忽略该记录 请帮忙 问候,, R您可以使用一个巨大的大小写表达式: select f1, (case when max(f10) is not null then 10 when max(f9) is not null then 9 when max(f8) is
R您可以使用一个巨大的
大小写
表达式:
select f1,
(case when max(f10) is not null then 10
when max(f9) is not null then 9
when max(f8) is not null then 8
when max(f7) is not null then 7
when max(f6) is not null then 6
when max(f5) is not null then 5
when max(f4) is not null then 4
when max(f3) is not null then 3
when max(f2) is not null then 2
end) as max_level
from t
where not (f2 <=> f3 and f3 <=> f4 and . . . )
group by f1;
选择f1,
(如果最大值(f10)不为空,则为10
当最大值(f9)不为空时,则为9
当最大值(f8)不为空时,则为8
当最大值(f7)不为空时,则为7
当最大值(f6)不为空时,则为6
当最大值(f5)不为空时,则为5
当最大值(f4)不为空时,则为4
当最大值(f3)不为空时,则为3
当最大值(f2)不为空时,则为2
结束)作为最高液位
从t
其中不包括(f2 f3和f3 f4和……)
按f1分组;
您的表没有最佳设计,理想情况下,F列应该显示在单独的行中,而不是列中。数据库表不是电子表格。您可能希望重新开始,或者放弃使用RDBMS的想法。