Counting how many MySQL fields in a row are filled (or empty)

后端 未结 3 2015
被撕碎了的回忆
被撕碎了的回忆 2021-02-09 05:59

I need to put together a method that would allow me to quantify how many fields in a row have been filled by a user.

For example:

User    Name    Age             


        
3条回答
  •  我在风中等你
    2021-02-09 06:38

    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
    

    Use the case according to what no info means: empty or null.

提交回复
热议问题