MySQL query to count non-null values in a single row

后端 未结 2 1525
孤城傲影
孤城傲影 2021-01-24 15:33

I\'m trying to put together a MYSQL query that will count the number of Non-Null (or better yet, non-zero) values in select fields in a single row and then sort from lowest to h

2条回答
  •  我在风中等你
    2021-01-24 15:58

    This should do what you want:

    SELECT ID, Name, Score_1, Score_2, Score_3
    FROM Table1
    ORDER BY (Score_1 = 0) + (Score_2 = 0) + (Score_3 = 0)
    

    Result:

    ID  Name   Score_1  Score_2  Score_3
    4   Mike   4        5        5      
    1   Dan    8        7        0      
    2   Joe    0        0        3      
    3   Chris  0        0        0      
    

提交回复
热议问题