查询从其继承的父表时获取行的源表的名称 [英] Get the name of a row's source table when querying the parent it inherits from

查看:15
本文介绍了查询从其继承的父表时获取行的源表的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Postgres 数据库,其中包含多个相互继承的表.我可以从父表中 SELECT 以从它的所有子表中获取结果,但需要获取每个结果源自的表的名称.

I have a Postgres database with several tables that inherit from one other. I can SELECT from the parent table to get results from all it's children, but need to get the name of the table that each result originates from.

此处找到的方法不起作用,因为我只是查询一张表,提前不知道结果中会出现哪些孩子.

The method found here does not work since I am only querying the one table, and do not know which children will be in the results ahead of time.

推荐答案

要识别特定行的源表,请使用 tableoid,就像您已经找到自己一样.
转换为 regclass 检索实际名称,根据当前 search_path 在需要的地方自动进行模式限定.

To identify the source table of a particular row, use the tableoid, like you found yourself already.
A cast to regclass retrieves the actual name, automatically schema-qualified where needed according to the current search_path.

SELECT *, tableoid::regclass::text AS table_name
FROM   master.tbl
WHERE  <some_condition>;

更多:

这篇关于查询从其继承的父表时获取行的源表的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆