2017-06-07 161 views
0

我在MYSQL中有一個類似於下面的查詢。該查詢給出了列名稱中沒有特定字符的表名列表。在MySQL中加入兩個查詢以將一個查詢的結果映射到另一個查詢

select distinct A.table_name from (select TABLE_NAME from information_schema.columns where TABLE_SCHEMA = 'testing' order by TABLE_NAME) A LEFT JOIN 
(select table_name from information_schema.columns where (COLUMN_NAME like '% %' or COLUMN_NAME like '%{%' or COLUMN_NAME like '%(%' or COLUMN_NAME like '%;%' 
or COLUMN_NAME like '%.%') and TABLE_SCHEMA = 'testing' order by TABLE_NAME) B ON (A.TABLE_NAME=B.TABLE_NAME) where B.table_name IS NULL 

上述查詢的結果:

100_test 
101_test 
102_backup 
103_backup 

我有另一個查詢象下面這樣:

select src_table,dst_table from test.mapping 

這裏test是數據庫,mapping是表名。

結果:

100_test 100_testing 
101_test 101_testing 
102_backup 102_backup_old 
103_backup 103_backup_old 
105_old_test 105_archived 

現在我想創建一個查詢,讓我src_table和dst_table從test.mapping1st查詢結果的每個表。

我想加入包括查詢這使我產生像下面

100_test 100_testing 
101_test 101_testing 
102_backup 102_backup_old 
103_backup 103_backup_old 

我怎樣才能做到這一點單查詢?

回答

1
select src_table,dst_table from test.mapping 
where src_table in 
(
select distinct A.table_name from (select TABLE_NAME from information_schema.columns where TABLE_SCHEMA = 'testing' order by TABLE_NAME) A LEFT JOIN 
(select table_name from information_schema.columns where (COLUMN_NAME like '% %' or COLUMN_NAME like '%{%' or COLUMN_NAME like '%(%' or COLUMN_NAME like '%;%' 
or COLUMN_NAME like '%.%') and TABLE_SCHEMA = 'testing' order by TABLE_NAME) B ON (A.TABLE_NAME=B.TABLE_NAME) where B.table_name IS NULL 
) 
相關問題