在 MySQL 中使用 coalesce() 显示第一个非空值?
mysqlmysqli database更新于 2024/11/15 14:36:17
coalesce() 可用于打印第一个非空列值。让我们首先创建一个表 −
mysql> create table DemoTable1927 ( StudentName varchar(20), StudentSubject varchar(20) ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1927 values('Chris','MySQL');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1927 values('David',NULL);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1927 values(NULL,'MongoDB');
Query OK, 1 row affected (0.00 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1927;
这将产生以下输出 −
+-------------+----------------+ | StudentName | StudentSubject | +-------------+----------------+ | Chris | MySQL | | David | NULL | | NULL | MongoDB | +-------------+----------------+ 3 rows in set (0.00 sec)
这是在 MySQL 中实现 coalesce() 的查询 −
mysql> select coalesce(StudentName,StudentSubject) as Result from DemoTable1927;
这将产生以下输出 −
+---------+ | Result | +---------+ | Chris | | David | | MongoDB | +---------+ 3 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

