mysql通过in (select table_name from )方式查询不到已经存在的表

系统环境

centos7.7+mysql5.6.46/mysql5.7.29

现象复现

准备测试库,test中的6张表
mysql> show tables;
±---------------+
| Tables_in_test|
±---------------+
| aa |
| bb |
| cc |
| dd |
| ff |
| testflashback2 |
±---------------+
创建一个测试表,里面存储三张表名
mysql> create table pwzy (table_name varchar(100));
mysql> insert into pwzy values(‘AA’),(‘EE’),(‘DD’);
mysql> select * from pwzy;
±-----------+
| table_name |
±-----------+
| AA |
| EE |
| DD |
±-----------+
通过not in方式查询不存在的表名

mysql> select table_name from pwzy where table_name not in (select table_name from information_schema.tables where table_schema=‘qjqx’);
±-----------+
| table_name |
±-----------+
| EE |
±-----------+
这里能查询到结果
通过 in方式查询存在的表名

mysql> select table_name from pwzy where table_name in (select table_name from information_schema.tables where table_schema=‘qjqx’);
Empty set (0.01 sec)
这里查询不到结果,期望得到的结果是AA,DD

mysql> select table_name from pwzy where table_name in (‘AA’,‘EE’);
±-----------+
| table_name |
±-----------+
| AA |
| EE |
±-----------+

解决方法

select a.table_name from pwzy a,(select table_name from information_schema.tables where table_schema=‘webdata’) b where a.table_name=b.table_name;
±-----------+
| table_name |
±-----------+
| test1 |
| test2 |
| test3 |
±-----------+

总结

猜你喜欢

转载自blog.csdn.net/weixin_41561946/article/details/107647190
今日推荐