PostgreSQL继承:获取记录class名
PostgreSQL inheritance: get the record class name
我有一个基础 table value_list 包含列 code, value、活跃。
我有一些 tables vl_a, vl_b, vl_c等继承自value_list.
有没有办法在执行 SELECT * FROM base
时知道 class 和 child 来自哪里。
换句话说,我想要:
code | value | active | class
-----+-------+---------+--------
1 | c | true | vl_a
3 | g | false | vl_b
5 | d | true | vl_a
7 | f | false | vl_c
2 | u | false | vl_c
2 | q | true | vl_b
8 | a | false | vl_a
这可能吗?
有关更多详细信息,请参见 tables:
CREATE TABLE value_list(
code integer NOT NULL,
value character varying(50),
active boolean,
CONSTRAINT pkey PRIMARY KEY (code)
)
CREATE TABLE vl_a() INHERITS (value_list);
CREATE TABLE vl_b() INHERITS (value_list);
CREATE TABLE vl_c() INHERITS (value_list);
字典不会让你这样做,但你可以手动合并:
select *,'vl_a' from vl_a
union all
select *,'vl_b' from vl_b
union all
select *,'vl_c' from vl_c
嗯,这给了它:
create or replace function uall() returns table ( code integer ,
value character varying(50),
active boolean,tablename text ) AS $$
declare
_i int;
_r record;
_t text := '';
begin
select distinct string_agg($s$select *,'$s$||table_name||$s$' from $s$||table_name,' union all ') into _t from information_schema.tables where table_name like 'vl_%';
return query execute _t;
end;$$ language plpgsql
;
select * from uall()
我终于在 Postgres doc 上找到了解决方案。
SELECT p.relname, vl.*
FROM qgep.is_value_list_base vl, pg_class p
WHERE vl.tableoid = p.oid;
我有一个基础 table value_list 包含列 code, value、活跃。 我有一些 tables vl_a, vl_b, vl_c等继承自value_list.
有没有办法在执行 SELECT * FROM base
时知道 class 和 child 来自哪里。
换句话说,我想要:
code | value | active | class
-----+-------+---------+--------
1 | c | true | vl_a
3 | g | false | vl_b
5 | d | true | vl_a
7 | f | false | vl_c
2 | u | false | vl_c
2 | q | true | vl_b
8 | a | false | vl_a
这可能吗?
有关更多详细信息,请参见 tables:
CREATE TABLE value_list(
code integer NOT NULL,
value character varying(50),
active boolean,
CONSTRAINT pkey PRIMARY KEY (code)
)
CREATE TABLE vl_a() INHERITS (value_list);
CREATE TABLE vl_b() INHERITS (value_list);
CREATE TABLE vl_c() INHERITS (value_list);
字典不会让你这样做,但你可以手动合并:
select *,'vl_a' from vl_a
union all
select *,'vl_b' from vl_b
union all
select *,'vl_c' from vl_c
嗯,这给了它:
create or replace function uall() returns table ( code integer ,
value character varying(50),
active boolean,tablename text ) AS $$
declare
_i int;
_r record;
_t text := '';
begin
select distinct string_agg($s$select *,'$s$||table_name||$s$' from $s$||table_name,' union all ') into _t from information_schema.tables where table_name like 'vl_%';
return query execute _t;
end;$$ language plpgsql
;
select * from uall()
我终于在 Postgres doc 上找到了解决方案。
SELECT p.relname, vl.*
FROM qgep.is_value_list_base vl, pg_class p
WHERE vl.tableoid = p.oid;