使用另一个表中的值映射数据库的表名

时间:2017-06-07 18:47:05

标签: mysql sql mapping

我在MySQL的test数据库中有一些表名。我在数据库mapping中有一个名为renamed的表,其中包含已重命名的表的信息。

现在我想要一个test数据库中的表列表及其在mapping表中重命名的名称。

test

中的表格
123_abc
124_abc
235_test

mapping表格中的信息

table       renamed

123_abc     123_abc_test
235_test    235_testing
abc_test    abc_test1

现在我想要一个列表,其中包含test数据库中的表名以及mapping数据库中renamed表中的重命名名称

预期结果是

123_abc     123_abc_test
235_test    235_testing

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以发出类似的

之类的MySQL语句

select * from renamed.mapping inner join information_schema.tables on renamed.mapping.table_name = information_schema.tables.TABLE_NAME where information_schema.tables.TABLE_SCHEMA='test'

答案 1 :(得分:0)

您可以使用属于MySQL的information_schema数据库,其中包含所有MySQL的元信息(包括表名),如下所示:

select t1.TABLE_NAME, t2.table_name 
from information_schema.`TABLES` t1 
where table_schema = 'test' 
inner join renamed.mapping t2 on 
    t1.table_name = t2.table_name

当然,使用内部联接,您只能获得实际重命名的表。如果您想要测试模式中的所有表,请改为执行左连接。