本文介绍了当列的顺序不同时创建 UNION ALL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 600 个表来执行 UNION ALL
查询.不幸的是,每个表中列的顺序各不相同,但它们将始终具有相同的名称 - 例如:
I have 600 tables to perform a UNION ALL
query on. Unfortunately the order of the columns in each table varies, however they will always have the same name - example:
表一
Item, Cost, ID, Code, Location
表 2
Cost, Id, Code, Location, Item
表 3
Id, Code, Cost, Item, Location
有没有办法编写联合查询,使其匹配列名,无论原始表中的顺序如何?
Is there a way to write the Union query so it will match the column names, no matter the order in the original table?
推荐答案
唉,没有.UNION ALL
按位置而不是名称.但是,您可以生成列:
Alas, no. UNION ALL
goes by position not by names. However, you can generate the columns:
select string_agg(column_name, ', ')
from information_schema.columns
where table_name = ? and
table_schema = ?;
然后您可以将列表插入到您的代码中.
You can then plug the list into your code.
这篇关于当列的顺序不同时创建 UNION ALL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!