为此,请使用COALESCE()。让我们首先创建一个表-
create table DemoTable1470 -> ( -> FirstName varchar(20), -> Age int -> );
使用插入命令在表中插入一些记录-
insert into DemoTable1470 values('Robert',23); insert into DemoTable1470 values('Bob',NULL); insert into DemoTable1470 values(NULL,25);
使用select语句显示表中的所有记录-
select * from DemoTable1470;
这将产生以下输出-
+-----------+------+ | FirstName | Age | +-----------+------+ | Robert | 23 | | Bob | NULL | | NULL | 25 | +-----------+------+ 3 rows in set (0.00 sec)
以下是选择字段的查询,如果为空,则选择另一个-
select coalesce(FirstName,Age) as FirstNameOrAgeValue from DemoTable1470;
这将产生以下输出-
+---------------------+ | FirstNameOrAgeValue | +---------------------+ | Robert | | Bob | | 25 | +---------------------+ 3 rows in set (0.00 sec)