要设置NOT NULL,请使用IS NOT NULL并找到值。语法如下-
select if('' is not NULL,1,0) as anyAliasName;
这是工作查询-
mysql> select if('' is not NULL,1,0);
这将产生以下输出-
+------------------------+ | if('' is not NULL,1,0) | +------------------------+ | 1 | +------------------------+ 1 row in set (0.00 sec)
要了解上述语法,让我们创建一个表-
mysql> create table DemoTable1915 ( Name varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1915 values('Chris'); mysql> insert into DemoTable1915 values(''); mysql> insert into DemoTable1915 values('David'); mysql> insert into DemoTable1915 values(NULL);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1915;
这将产生以下输出-
+-------+ | Name | +-------+ | Chris | | | | David | | NULL | +-------+ 4 rows in set (0.00 sec)
这是要实现IF()
并将NOT 1 NULL设置为1的查询-
mysql> select if(Name IS NOT NULL,1,0) as Result from DemoTable1915;
这将产生以下输出-
+--------+ | Result | +--------+ | 1 | | 1 | | 1 | | 0 | +--------+ 4 rows in set (0.00 sec)