在MySQL中使用CASE WHEN获取表是否存在的布尔结果

Get boolean result whether table exists or not using CASE WHEN in MySQL

1
2
3
4
5
6
mysql> create table DemoTable
 -> (
 -> Id int,
 -> Name varchar(20)
 -> );
Query OK, 0 rows affected (1.57 sec)

使用insert命令在表中插入一些记录-

1
2
3
4
mysql> insert into DemoTable values(101,'Chris');
Query OK, 1 row affected (0.71 sec)
mysql> insert into DemoTable values(102,'David');
Query OK, 1 row affected (0.20 sec)

使用select语句显示表中的所有记录-

1
mysql> select *from DemoTable;

这将产生以下输出-

1
2
3
4
5
6
7
+------+-------+
|  Id | Name |
+------+-------+
| 101 | Chris |
| 102 | David |
+------+-------+
2 rows in set (0.00 sec)

这是检查表是否存在的查询-

1
2
3
mysql> select max(case when table_name = 'DemoTable' then 'Yes the table
exist(TRUE)' else 'No(FALSE)' end) AS isTableExists
 -> from information_schema.tables;

这将产生以下输出-

1
2
3
4
5
6
+-------------------------------+
| isTableExists         |
+-------------------------------+
| Yes the table exist(TRUE)   |
+-------------------------------+
1 row in set (0.52 sec)