mysql – 选择Id为值为null的第一个列名

我想在按Id选择时获取第一个空值列名:

我试过了:

SELECT Snap1,Snap2,Snap3,Snap4
where NULL
FROM tbtProgressSnap
where Id=3

Id | Snap1 | Snap2 | Snap3 | Snap4|
-----------------------------------
1  |       |       |/a.png |/b.png|
-----------------------------------
2  | d.png | m.png |       |      |
-----------------------------------
3  | g.png |l.png  |p.png  |      |

我希望得到各自的ID

1 Snap1
2 Snap3
3 Snap4

最佳答案 试试这个:

SELECT WorkId, 
      (CASE WHEN Snap1 IS NULL OR snap1 = '' THEN 'Snap1'
            WHEN Snap2 IS NULL OR snap2 = '' THEN 'Snap2'
            WHEN Snap3 IS NULL OR snap3 = '' THEN 'Snap3'
            WHEN Snap4 IS NULL OR snap4 = '' THEN 'Snap4'
            WHEN Snap5 IS NULL OR snap5 = '' THEN 'Snap5'
            ELSE ''
       END) AS snapValue
FROM tbtProgressSnap 
WHERE WorkId=4;

检查SQL FIDDLE DMEO

OUTPUT

| WORKID | SNAPVALUE |
|--------|-----------|
|      4 |     Snap5 |
点赞