2012-01-09 61 views
1

我需要编写一个查询语句,如果我要求的所有表格都存在,将返回结果。我知道这是存在的:查询是否存在多个表格

show tables like 'user' 

只是不知道如何扩展多表。我知道这是无效的代码,但类似

show tables like 'user' AND show tables like 'site' AND ... 

谢谢!

+0

可以使用INFORMATION_SCHEMA.TABLES对象 - 见http://dev.mysql.com/doc/refman/5.0/en/tables-table.html – dash 2012-01-09 23:01:12

回答

3
select * 
from information_schema.tables 
where table_name in ('tb1','tb2','tb3') 
and table_schema = 'your_db' 
having count(table_name) = 3 

嗯,我意识到有一个与该查询的问题。如果所有表存在,它只返回一条记录。这将是一个更好的解决方案

select group_concat(table_name order by table_name) as table_list 
from information_schema.tables 
where table_name in ('tb1','tb2','tb3') 
and table_schema = 'your_db' 
having count(*) = 3 
+0

+1为完整的例子。像化身;-) – dash 2012-01-09 23:03:51

+0

堆栈溢出是一个神奇的地方。神圣废话,这个答案很快!谢谢!! – elightbo 2012-01-09 23:06:19

+0

@dash。谢谢。附:埃里克规则,大声笑! ;) – 2012-01-09 23:11:42

1

我知道这个作品在MySQL的(只是一个例子)

SHOW TABLES WHERE Tables_in_<database-name> IN ([<table-names-comma-separated>]) 

例子:

SHOW TABLES WHERE Tables_in_YourDB IN ('users', 'sites') 
+0

+ 1我没不知道SHOW TABLES命令 - 谢谢! – dash 2012-01-09 23:04:48

+0

谢谢你给我正确的方式。 – elightbo 2012-01-09 23:23:41