即使在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)
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西