Mysql将多个表联合成一个大表

时间:2013-01-01 00:11:09

标签: mysql union

我有一个查询选择多个表的名称:

SELECT TABLE_NAME表格来自INFORMATION_SCHEMA.TABLES

+---------------+
| TABLES        |
+---------------+
| A             |
| B             |
| <more tables> |
+---------------+

我想创建一个查询,其中每个表中的每个值都联合在一起:

+------------+----+----------+
| table_name | id | name     |
+------------+----+----------+
| A          | 1  | item1    | <- Items from Table A
| A          | 2  | item2    | 
| A          | 3  | item3    |
| B          | 1  | item1    | <- Items from Table B
| B          | 2  | item2    |
| <entries for other tables> |
+------------+----+----------+

任何想法?提前谢谢!

1 个答案:

答案 0 :(得分:1)

假设你在最宽松的意义上使用“聚合”......

select 'A' as table_name, id, name
from table_a
union all
select 'B' as table_name, id, name
from table_b