如果列为空,MySQL 查询将返回特定的字符串?
mysqlmysqli database更新于 2025/8/20 14:04:17
首先我们创建一个表 −
mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.85 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(NULL);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(NULL);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+-------+ | Name | +-------+ | John | | NULL | | NULL | | David | +-------+ 4 rows in set (0.00 sec)
如果列为空,则返回特定字符串的查询 −
mysql> SELECT COALESCE(Name, 'Not Valid') from DemoTable;
输出
+-----------------------------+ | COALESCE(Name, 'Not Valid') | +-----------------------------+ | John | | Not Valid | | Not Valid | | David | +-----------------------------+ 4 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

