我有一组要合并在一起的表。如果我需要添加更多内容,我希望以后可以返回并访问“原始”数据。我通过向引用表添加引用列来做到这一点,该引用列将包含数据所来自的表的表名。
从表查询时如何访问表的名称?
编辑:详细信息:
我正在创建的表如下所示:
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name
thing1.shape
FROM
public.thing1_source_table
UNION
SELECT
thing2.name
thing2.shape
FROM
public.thing2_source_table);
我想添加“源”字段:
ALTER TABLE combined_things ADD COLUMN source_id character varying(100);
ALTER TABLE comnined_things SET COLUMN source_id = {table_name}
而且我不知道如何拉{table_name}
最佳答案
您可以在创建表时将它们添加为字符串常量
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name,
thing1.shape,
CAST('public.thing1_source_table' AS CHAR(100)) source_id
FROM
public.thing1_source_table
UNION
SELECT
thing2.name,
thing2.shape,
'public.thing2_source_table'
FROM
public.thing2_source_table);
请注意,无法将列强制转换为varchar
关于mysql - 如何在功能上访问SQL表的表名?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37658258/