知道哪些表有列" a"和" b"使用SQL查询

时间:2017-07-28 21:31:59

标签: mysql

我希望得到一个名为" a"和另一个名为" b"

的专栏

例如:

SELECT DISTINCT tables.name FROM tables_colums INNER JOIN tables INNER JOIN colums 
ON tables.id = tables_colums.table_id AND colums.id = tables_colums.column_id 
WHERE columns.name = "a" AND columns.name = "b"

1 个答案:

答案 0 :(得分:2)

您可以在information_schema数据库中找到元数据。列的medadata存储在information_schema.columns表中。

select TABLE_SCHEMA, TABLE_NAME
from information_schema.columns
where COLUMN_NAME in ('a', 'b')
group by TABLE_SCHEMA, TABLE_NAME
having count(*) = 2

如果您只需要来自特定数据库(架构)的表:

select TABLE_NAME
from information_schema.columns
where TABLE_SCHEMA = 'my_db'
  and COLUMN_NAME in ('a', 'b')
group by TABLE_NAME
having count(*) = 2

live demo