计算连续多少个MySQL字段被填充(或为空)

pep*_*epe 6 php mysql sql codeigniter

我需要整理一个方法,以便我可以量化用户填充的行数.

例如:

User    Name    Age    Country    Gender    Height
1       Mike    34     USA        Male      6
2       Bill    23     CA                   5
3       Jane    31     USA        
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,我想查询数据库并返回一个值,该值将反映用户记录的完成程度.如:

User 1 = 100% complete
User 2 = 80% complete
User 3 = 60% complete
Run Code Online (Sandbox Code Playgroud)

我想知道是否需要通过SQL子句来完成,或者如果通过PHP SQL函数,可以查询数据库并计算完成程度.

有什么建议怎么做?我使用PHP 5(codeigniter)和SQL 5.0.77,但任何路线图都将非常感激.

Clo*_*eto 9

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
Run Code Online (Sandbox Code Playgroud)

根据没有信息的含义使用案例:空或null.


Pau*_*aul 8

$result = mysql_query('SELECT * FROM `MyTable`');
while($row = mysql_fetch_row($result)){
    $empty_count = 0;
    $count = count($row);
    for($i = 0; $i < $count; $i++)
        if($row[$i] === '' || $row[$i] === 'NULL')
            $empty_count++;
    echo 'User '.$row[0].' = '.((int)(100*(1-$empty_count/($count-1)))).'% complete';
}
Run Code Online (Sandbox Code Playgroud)