使用EXISTS在MySQL中检查表是否为空

以下是使用MySQL EXISTS检查表是否为空的语法-

SELECT EXISTS(SELECT 1 FROM yourTableName);

示例

首先,让我们创建一个表。创建表的查询如下-

mysql> create table ReturnDemo

   -> (

   -> Id int,

   -> Name varchar(10)

   -> );

使用insert命令在表中插入一些记录。查询如下-

mysql> insert into ReturnDemo values(100,'Larry');

mysql> insert into ReturnDemo values(101,'Bob');

mysql> insert into ReturnDemo values(102,'Sam');

使用select语句显示表中的所有记录。查询如下-

mysql> select *from ReturnDemo;

输出结果

+------+-------+

| Id   | Name |

+------+-------+

|  100 | Larry |

|  101 |   Bob |

|  102 |   Sam |

+------+-------+

3 rows in set (0.00 sec)

这是检查MySQL表是否为空的查询-

mysql> select exists(select 1 from ReturnDemo) AS Output;

输出结果

+--------+

| Output |

+--------+

|      1 |

+--------+

1 row in set (0.00 sec)

输出1表示MySQL表不为空。

以上是 使用EXISTS在MySQL中检查表是否为空 的全部内容, 来源链接: utcz.com/z/341079.html

回到顶部