即使在MySQL查询中没有结果,也返回一个值?
您可以使用IFNULL()MySQL中的函数来返回一个值,即使没有结果也是如此。让我们创建一个表。查询创建表。
mysql> create table IfNullDemo −> ( −> Id int, −> Name varchar(100) −> );
借助insert命令将一些记录插入表中。查询如下-
mysql> insert into IfNullDemo values(1,'John'); mysql> insert into IfNullDemo values(200,'Sam'); mysql> insert into IfNullDemo values(204,'Carol'); mysql> insert into IfNullDemo values(510,'Johnson');
在select语句的帮助下显示表中的所有记录。查询如下-
mysql> select *from IfNullDemo;
以下是输出-
+------+---------+ | Id | Name | +------+---------+ | 1 | John | | 200 | Sam | | 204 | Carol | | 510 | Johnson | +------+---------+ 4 rows in set (0.00 sec)
让我们首先返回TRUE条件的值-
查询如下-
mysql> select ifnull((select Id from IfNullDemo where Id = 200),'No Result Found') As ResultFound;
以下是输出-
+-------------+ | ResultFound | +-------------+ | 200 | +-------------+ 1 row in set (0.00 sec)
现在,让我们使用IFNULL方法返回没有结果的值。查询如下-
mysql> select ifnull((select Id from IfNullDemo where Id = 400),'No Result Found') As ResultFound;
以下是输出-
+-----------------+ | ResultFound | +-----------------+ | No Result Found | +-----------------+ 1 row in set (0.00 sec)
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志