比较NULL值并在新的MySQL列中为这些值显示值1?
为此,请IF()
与ISNULL属性一起使用。让我们首先创建一个表-
mysql> create table DemoTable ( Name varchar(100), CountryName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Chris','US'); mysql> insert into DemoTable values('Mike',NULL); mysql> insert into DemoTable(Name) values('David'); mysql> insert into DemoTable values('Bob','AUS');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------+-------------+ | Name | CountryName | +-------+-------------+ | Chris | US | | Mike | NULL | | David | NULL | | Bob | AUS | +-------+-------------+ 4 rows in set (0.00 sec)
以下是查询以比较NULL值并在新的MySQL列中显示这些值的值1-
mysql> select Name,CountryName,If (CountryName IS NULL,1,0) AS CountDemo from DemoTable;
这将产生以下输出-
+-------+-------------+-----------+ | Name | CountryName | CountDemo | +-------+-------------+-----------+ | Chris | US | 0 | | Mike | NULL | 1 | | David | NULL | 1 | | Bob | AUS | 0 | +-------+-------------+-----------+ 4 rows in set (0.00 sec)