我需要组合一种方法,使我能够量化用户填写了一行中的多少个字段。
例如:
User Name Age Country Gender Height
1 Mike 34 USA Male 6
2 Bill 23 CA 5
3 Jane 31 USA
在上面的例子中,我想查询数据库并返回一个值来反射(reflect)用户记录的完成程度。如:
User 1 = 100% complete
User 2 = 80% complete
User 3 = 60% complete
我想知道这是否需要通过 SQL 子句完成,或者是否可以通过 PHP SQL 函数查询数据库并计算完成度。
有什么建议如何做到这一点?我正在使用 PHP 5 (codeigniter) 和 SQL 5.0.77,但任何路线图将不胜感激。
最佳答案
select
User,
(
case Name when '' then 0 else 1 end
+
case when Age is null then 0 else 1 end
+
case Country when '' then 0 else 1 end
+
case Gender when '' then 0 else 1 end
+
case when Height is null then 0 else 1 end
) * 100 / 5 as complete
根据 no info 的含义使用大小写:empty 或 null。
关于php - 计算一行中有多少个 MySQL 字段被填充(或为空),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6720725/