[英]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.