现在是这么个情况
数据库命名规则是db1,db2,db3,db4,db5~~~~~~~~一直到下去,最大是db10
数据库如果存在,则里面一定会有一个table1表,且表结构是一样的现在我想查询出所有db中的table1的记录
这么查?能否先判断数据库是否存在,然后再查?SELECT * FROM db1.table1 IF EXISTS db1.table1
说有语法错误。
数据库命名规则是db1,db2,db3,db4,db5~~~~~~~~一直到下去,最大是db10
数据库如果存在,则里面一定会有一个table1表,且表结构是一样的现在我想查询出所有db中的table1的记录
这么查?能否先判断数据库是否存在,然后再查?SELECT * FROM db1.table1 IF EXISTS db1.table1
说有语法错误。
我本来是想仿照drop if exists
谁知道用在select上不行
如果可以的话,比在程序中要简单的多吧
你可以通过查询 mysql> select SCHEMA_NAME from information_schema.SCHEMATA;
+--------------------+
| SCHEMA_NAME |
+--------------------+
| information_schema |
| csdn |
| mysql |
| test |
+--------------------+
4 rows in set (0.09 sec)mysql>
select 'db1' as dbName,* from db1.table1
union all
select 'db2' as dbName,* from db2.table1
union all
select 'db3' as dbName,* from db3.table1
union all
select 'db4' as dbName,* from db4.table1
union all
...
union all
select 'dbN' as dbName,* from dbN.table1以后直接 select * from xxx where dbName='db1'
现在的关键点是我不知道dbn是否存在
如果不存在,则这个视图就建不起来
我就想能不能像drop table一样,先判断是否存在,如果存在则执行
如:
***
union all
select 'db4' as dbName,* from db4.table1 if exists db4.table1
***
但是报错