简体   繁体   English

我们如何在 mysql 中的另一个表中查找一个表的列名

[英]How can we lookup column names of one table with data in another table in mysql

If any one can help me如果有人可以帮助我

I have scenario were I need to take the col name of one table (there is no data only the col name) and then search those col name in the data available in another table.我的情况是我需要获取一个表的列名(没有数据只有列名),然后在另一个表中可用的数据中搜索这些列名。

I can manually do it like take a col name and then search in another table using like and I see it exist.我可以手动执行它,例如取一个 col 名称,然后使用 like 在另一个表中搜索,我发现它存在。 But can some help me know how we can write a SQL script for this.但是有人可以帮助我知道我们如何为此编写 SQL 脚本。

SQL: SQL:

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'TableName'

MYSQL MYSQL

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='yourdatabasename' 
    AND `TABLE_NAME`='yourtablename';

if you are looking to compare the entire database it could work like this, you can restrict it to single table as well如果您想比较整个数据库,它可以像这样工作,您也可以将其限制为单个表

        SELECT * FROM
        (SELECT 
                    c.TABLE_SCHEMA_S,
                    c.TABLE_SCHEMA_D,
                    c.TABLE_NAME_S,
                    c.TABLE_NAME_D,
                    GROUP_CONCAT(c.COLUMN_NAME)  COLUMN_NAMES
            FROM
                (SELECT DISTINCT
                    a.TABLE_SCHEMA TABLE_SCHEMA_S,
                    b.TABLE_SCHEMA TABLE_SCHEMA_D,
                    a.TABLE_NAME TABLE_NAME_S,
                    b.TABLE_NAME TABLE_NAME_D,
                    b.COLUMN_NAME
            FROM
                information_schema.COLUMNS a
            INNER JOIN information_schema.COLUMNS b ON a.TABLE_NAME = b.TABLE_NAME
                AND a.COLUMN_NAME = b.COLUMN_NAME
            WHERE
                a.TABLE_SCHEMA = 'first-db-name'
                    AND b.TABLE_SCHEMA = 'second-db-name') c
            GROUP BY c.TABLE_NAME_S) d;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySql中如何将列数据从一张表复制到另一张表? - How to Copy Column data from one table to another table in MySql? 在单个MySQL查询中查找另一张表中一列的值 - In a single MySQL query lookup one column's value in another table 如何在同一表上的mysql中将数据复制到另一列? - how to copy one column to another with data in mysql on same table? 从一个表的列到另一表的列的mysql数据减法 - mysql data substraction from column of one table to column of another table 如何在MySQL中按另一个表中的列对一个表排序? - How can I sort one table by a column in another in MySQL? 当字符串是使用 MySQL 的表中另一列中的数据时,如何在表的一列中搜索字符串 - How do I search one column of a table for a string when the string is the data in another column in that table using MySQL MySQL-从表的一列中获取数据并将其插入到另一表中 - MySQL - Taking data from one column in a table and inserting it into another table 如何通过匹配列名将列数据从一个表复制到另一个表? 在 SQL 或 Power Query 中 - How to Copy column data from one table to another by matching Column names? in SQL or Power Query 如何将表的一列中的字符串数组中的数据与表中另一列中的 JSON 数据进行比较? - How can I compare the data in an array of strings that is in one column of a table to JSON data within another column in the table? mysql中如何将表列数据添加到另一个类似的列表中 - How to add table column data to another similar column table in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM