让我们首先创建一个表-mysql> create table DemoTable839(
StudentFirstName varchar(100),
StudentLastName varchar(100)
);
使用插入命令在表中插入一些记录-mysql> insert into DemoTable839 values('Chris','Brown');
mysql> insert into DemoTable839 values('','Taylor');
mysql> insert into DemoTable839 values(NULL,'Taylor');
mysql> insert into DemoTable839 values('Adam','Smith');
使用select语句显示表中的所有记录-mysql> select *from DemoTable839;
这将产生以下输出-+------------------+-----------------+
| StudentFirstName | StudentLastName |
+------------------+-----------------+
| Chris | Brown |
| | Taylor |
| NULL | Taylor |
| Adam | Smith |
+------------------+-----------------+
4 rows in set (0.00 sec)
以下是查询,仅从表中返回非空和非空值,并用相应的列值填充空值和空值-mysql> select if(length(StudentFirstName),StudentFirstName,StudentLastName) from DemoTable839;
这将产生以下输出-+---------------------------------------------------------------+
| if(length(StudentFirstName),StudentFirstName,StudentLastName) |
+---------------------------------------------------------------+
| Chris |
| Taylor |
| Taylor |
| Adam |
+---------------------------------------------------------------+
4 rows in set (0.00 sec)