gpt4 book ai didi

sql - Postgres : select data from parent table and all child tables

转载 作者:行者123 更新时间:2023-11-29 12:14:24 25 4
gpt4 key购买 nike

我有一个父表和几个子表,它们的结构与父表和彼此之间略有不同。我可以组装一个查询,从父表和所有子表中获取所有数据以及所有附加列吗?

最佳答案

使用适当的模式并使用 INHERITS 将结构告知 PostgreSQL:

CREATE TABLE foo(x int);

CREATE TABLE bar(y int) INHERITS (foo); -- <===

INSERT INTO foo(x) VALUES(1); -- one record
INSERT INTO bar(x,y) VALUES(2,3); -- one record

SELECT * FROM foo; -- two results

SELECT * FROM ONLY foo; -- one result, see ONLY

http://www.postgresql.org/docs/current/interactive/ddl-inherit.html

关于sql - Postgres : select data from parent table and all child tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9114490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com