在每个表中搜索多个列名,并返回表名

时间:2016-09-12 09:20:08

标签: mysql

我尝试编写单个查询,以返回数据库中每个表的表名,该表有3个列名:gps time, shape, id。 我一直在寻找答案,但一切只适用于一个列名。

SELECT table_name 
FROM information_schema.COLUMNS 
WHERE 
TABLE_SCHEMA = 'db_name' 
AND COLUMN_NAME = 'column_name'

当我添加另一个"AND COLUMN_NAME = 'column_name2'"时,它返回0.

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

SELECT table_name 
FROM information_schema.COLUMNS 
WHERE 
TABLE_SCHEMA = 'db_name' 
AND COLUMN_NAME IN ('column_name', 'column_name2', 'column_name3')
GROUP BY table_name
HAVING COUNT(*) = 3

答案 1 :(得分:1)

尝试以下操作:

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS c1
JOIN INFORMATION_SCHEMA.COLUMNS c2 ON c1.TABLE_NAME=c2.TABLE_NAME
JOIN INFORMATION_SCHEMA.COLUMNS c3 ON c2.TABLE_NAME=c3.TABLE_NAME 
                                  AND c1.TABLE_NAME=c3.TABLE_NAME
WHERE c1.COLUMN_NAME = 'gps time'
  AND c2.COLUMN_NAME = 'shape'
  AND c3.COLUMN_NAME = 'id'