这是我的第二个(我称之为noobish)sql问题,虽然我知道我应该提高我的sql知识,在这种情况下,我已经离开了一个开发人员.
无论如何,这个问题.我正在使用mysql 5
我正在处理商店程序api.其中一个sp是返回表的所有行以及来自所述表的一些其他计算结果.
Table: reports
ID|col_1|col_2|col_3|col_4|col_5
--------------------------------
x| xxx | xxx | xxx | xxx | xxx
Run Code Online (Sandbox Code Playgroud)
我正在尝试计算完成索引(之前的开发人员没有为此添加一列,时间不允许我重构).
因此,如果...
col_1 is not null
col_2 is not null
col_3 is null
...
Run Code Online (Sandbox Code Playgroud)
舞台将是2.
而如果...
col_1 is not null
col_2 is null <<-- notice the empty field
col_3 is not null
Run Code Online (Sandbox Code Playgroud)
舞台将1是一个空场.
所以舞台增加直到有一个空场.
现在我知道我可以检索所有行并使用php处理这些信息,但我不想转向系统的设计.
如果有人能指出我正确的方向,我会很感激.
Select Case
When col_1 Is Null Then 0
When col_2 Is Null Then 1
When col_3 Is Null Then 2
When col_4 Is Null Then 3
When col_5 Is Null Then 4
Else 5
End
Run Code Online (Sandbox Code Playgroud)