Here is one way:
select id, ((var1 is null) + (var2 is null) + (var3 is null)) as var4
from table t;
MySQL treats booleans as integers, with true being 1
and false being 0
. You can just add them up to get the total.
As an update:
update table t
set var4 = ((var1 is null) + (var2 is null) + (var3 is null));
As a note, MySQL doesn't support ISNULL()
. That is more of a SQL Server function. But it is not ANSI standard anyway, so you are usually better off using coalesce()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…