如何找到 MySQL 中所有具有特定列名的表?

发布于 2024-07-06 08:43:01 字数 58 浏览 16 评论 0原文

我有 2-3 个不同的列名,我想在整个数据库中查找它们并列出具有这些列的所有表。 有没有简单的脚本?

I have 2-3 different column names that I want to look up in the entire database and list out all tables which have those columns. Is there any easy script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

风筝在阴天搁浅。 2024-07-13 08:43:01

要获取数据库 YourDatabase 中包含列 columnAColumnB 的所有表:

SELECT DISTINCT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME IN ('columnA','ColumnB')
        AND TABLE_SCHEMA='YourDatabase';

To get all tables with columns columnA or ColumnB in the database YourDatabase:

SELECT DISTINCT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME IN ('columnA','ColumnB')
        AND TABLE_SCHEMA='YourDatabase';
桃酥萝莉 2024-07-13 08:43:01
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%wild%';
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%wild%';
习ぎ惯性依靠 2024-07-13 08:43:01

更简单地用一行 SQL 完成:

SELECT * FROM information_schema.columns WHERE column_name = 'column_name';

More simply done in one line of SQL:

SELECT * FROM information_schema.columns WHERE column_name = 'column_name';
梅窗月明清似水 2024-07-13 08:43:01
SELECT DISTINCT TABLE_NAME, COLUMN_NAME  
FROM INFORMATION_SCHEMA.COLUMNS  
WHERE column_name LIKE 'employee%'  
AND TABLE_SCHEMA='YourDatabase'
SELECT DISTINCT TABLE_NAME, COLUMN_NAME  
FROM INFORMATION_SCHEMA.COLUMNS  
WHERE column_name LIKE 'employee%'  
AND TABLE_SCHEMA='YourDatabase'
九八野马 2024-07-13 08:43:01

在较旧的 MySQL 版本或某些没有 information_schema 的 MySQL NDB Cluster 版本中,您可以转储表结构并手动搜索列。

mysqldump -h$host -u$user -p$pass --compact --no-data --all-databases > some_file.sql

现在使用您喜欢的文本编辑器搜索 some_file.sql 中的列名称,或者使用一些漂亮的 AWK 脚本。


以及一个简单的 sed 脚本来查找该列。 只需将 COLUMN_NAME 替换为您的:

sed -n '/^USE/{h};/^CREATE/{H;x;s/\nCREATE.*\n/\n/;x};/COLUMN_NAME/{x;p};' <some_file.sql
USE `DATABASE_NAME`;
CREATE TABLE `TABLE_NAME` (
  `COLUMN_NAME` varchar(10) NOT NULL,

您可以直接在 sed 中通过管道传输转储,但这很简单。

In older MySQL versions or some MySQL NDB Cluster versions that do not have information_schema, you can dump the table structure and search the column manually.

mysqldump -h$host -u$user -p$pass --compact --no-data --all-databases > some_file.sql

Now search the column name in some_file.sql using your preferred text editor, or use some nifty AWK scripts.


And a simple sed script to find the column. Just replace COLUMN_NAME with yours:

sed -n '/^USE/{h};/^CREATE/{H;x;s/\nCREATE.*\n/\n/;x};/COLUMN_NAME/{x;p};' <some_file.sql
USE `DATABASE_NAME`;
CREATE TABLE `TABLE_NAME` (
  `COLUMN_NAME` varchar(10) NOT NULL,

You can pipe the dump directly in sed, but that's trivial.

萌辣 2024-07-13 08:43:01

对于那些搜索相反的内容,即寻找不包含特定列名的表,这是查询...

SELECT DISTINCT TABLE_NAME FROM information_schema.columns WHERE 
TABLE_SCHEMA = 'your_db_name' AND TABLE_NAME NOT IN (SELECT DISTINCT 
TABLE_NAME FROM information_schema.columns WHERE column_name = 
'column_name' AND TABLE_SCHEMA = 'your_db_name');

当我们开始慢慢实现使用 InnoDB 的特殊 ai_col 列,需要找出 200 个表中哪些表尚未升级。

For those searching for the inverse of this, i.e. looking for tables that do not contain a certain column name, here is the query...

SELECT DISTINCT TABLE_NAME FROM information_schema.columns WHERE 
TABLE_SCHEMA = 'your_db_name' AND TABLE_NAME NOT IN (SELECT DISTINCT 
TABLE_NAME FROM information_schema.columns WHERE column_name = 
'column_name' AND TABLE_SCHEMA = 'your_db_name');

This came in really handy when we began to slowly implement use of InnoDB's special ai_col column and needed to figure out which of our 200 tables had yet to be upgraded.

花期渐远 2024-07-13 08:43:01

使用这一行查询。 将 desired_column_name 替换为您的列名称。

SELECT TABLE_NAME FROM information_schema.columns WHERE column_name = 'desired_column_name';

Use this one line query. Replace desired_column_name by your column name.

SELECT TABLE_NAME FROM information_schema.columns WHERE column_name = 'desired_column_name';
长途伴 2024-07-13 08:43:01

如果您想“仅获取所有表”,则使用此查询:

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME like '%'
and TABLE_SCHEMA = 'tresbu_lk'

如果您想“获取所有包含列的表”,则使用此查询:

SELECT DISTINCT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE '%'
AND TABLE_SCHEMA='tresbu_lk'

If you want to "get all tables only", then use this query:

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME like '%'
and TABLE_SCHEMA = 'tresbu_lk'

If you want "to get all tables with columns", then use this query:

SELECT DISTINCT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE '%'
AND TABLE_SCHEMA='tresbu_lk'
贱人配狗天长地久 2024-07-13 08:43:01
select distinct table_name 
from information_schema.columns 
where column_name in ('ColumnA') 
    and table_schema='YourDatabase';
    and table_name in 
    (
        select distinct table_name 
        from information_schema.columns 
        where column_name in ('ColumnB')
              and table_schema='YourDatabase';
    );

^^ 将获得具有 ColumnA ColumnB 的表格,而不是像接受的答案那样具有 ColumnA ColumnB

select distinct table_name 
from information_schema.columns 
where column_name in ('ColumnA') 
    and table_schema='YourDatabase';
    and table_name in 
    (
        select distinct table_name 
        from information_schema.columns 
        where column_name in ('ColumnB')
              and table_schema='YourDatabase';
    );

That ^^ will get the tables with ColumnA and ColumnB instead of ColumnA or ColumnB like the accepted answer

乖乖 2024-07-13 08:43:01
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%city_id%' AND TABLE_SCHEMA='database'
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%city_id%' AND TABLE_SCHEMA='database'
维持三分热 2024-07-13 08:43:01

information_schema 的问题是它可能非常慢。 使用 SHOW 命令速度更快。

选择数据库后,首先发送查询 SHOW TABLES。 然后对每个表执行 SHOW COLUMNS。

在 PHP 中,看起来像这样

    $res = mysqli_query("SHOW TABLES");
    while($row = mysqli_fetch_array($res))
    {   $rs2 = mysqli_query("SHOW COLUMNS FROM ".$row[0]);
        while($rw2 = mysqli_fetch_array($rs2))
        {   if($rw2[0] == $target)
               ....
        }
    }

The problem with information_schema is that it can be terribly slow. It is faster to use the SHOW commands.

After you select the database you first send the query SHOW TABLES. And then you do SHOW COLUMNS for each of the tables.

In PHP that would look something like

    $res = mysqli_query("SHOW TABLES");
    while($row = mysqli_fetch_array($res))
    {   $rs2 = mysqli_query("SHOW COLUMNS FROM ".$row[0]);
        while($rw2 = mysqli_fetch_array($rs2))
        {   if($rw2[0] == $target)
               ....
        }
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文