我有一个 Postgres 数据库,其中有几个相互继承的表。我可以SELECT
从父表获取其所有子表的结果,但需要获取每个结果源自的表的名称。
找到的方法here https://stackoverflow.com/questions/17016549/get-table-name-in-postgres-query-result不起作用,因为我只查询一张表,并且不知道哪些子项将提前出现在结果中。
要识别特定行的源表,请使用tableoid https://www.postgresql.org/docs/current/ddl-system-columns.html,就像你已经找到了自己一样。
演员表regclass https://www.postgresql.org/docs/current/datatype-oid.html检索实际名称,根据当前需要自动进行模式限定search_path
.
SELECT *, tableoid::regclass::text AS table_name
FROM master.tbl
WHERE <some_condition>;
More:
-
根据表值找出哪个架构 https://stackoverflow.com/questions/12902072/find-out-which-schema-based-on-table-values/12902441#12902441
- 使用 Postgres 从多个模式中选择(检索)所有记录 https://stackoverflow.com/questions/20575610/retrieve-all-records-from-multiple-schemas-in-a-postgresql-database/20576324#20576324
- search_path 如何影响标识符解析和“当前模式” https://stackoverflow.com/questions/9067335/how-does-the-search-path-influence-identifier-resolution-and-the-current-schema/9067777#9067777
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)