在Postgresql中选择具有继承的表名

问题描述 投票:0回答:1

所以这是我的父表:TableA

 +-----+-----------+
 + id  +   name    +
 +-----+-----------+

这是继承tableA:TableT TableT的表

+-----+-----------+----------+
+ id  +   date    +   year   +
+-----+-----------+----------+

+-----+-----------+----------+
+ id  +   owner   +   age    +
+-----+-----------+----------+

我有一个Select我在TableA中从名称中获取id,我需要的是获取包含该特定id的表的名称。我想过制作一个观点,但我是一个noobie,我不知道如何。

谢谢。

sql postgresql inheritance view
1个回答
0
投票

你可以使用system column tableoid

以下示例:

create table table_a (id integer, name text);
create table table_b () inherits (table_a);
create table table_c () inherits (table_a);

insert into table_b values (1, 'one');
insert into table_c values (2, 'two');

select tableoid::regclass as table_name, id, name
from table_a;

收益:

table_name | id | name
-----------+----+-----
table_b    |  1 | one 
table_c    |  2 | two 
© www.soinside.com 2019 - 2024. All rights reserved.