小编fre*_*111的帖子

查询父表并获取子表列

我有两张表,一张继承了另一张:

CREATE TABLE parent (
    col1 INTEGER
);

CREATE TABLE child (
    col2 INTEGER
) INHERITS( parent );

// Insert some data
INSERT INTO parent( col1 ) VALUES( 1 );
INSERT INTO child( col1, col2 ) VALUES( 2, 2 );
Run Code Online (Sandbox Code Playgroud)

有什么方法可以查询父表,使其获取子表的所有列?

此查询仅返回父列:

SELECT * FROM parent;

| col1 |
| ---- |
| 1    |
| 2    |
Run Code Online (Sandbox Code Playgroud)

我想要的查询将返回父和子中的所有列:

SELECT ...  -- some query, with desired result:

| col1 | col2 |
| ---- | ---- |
| 1    | …
Run Code Online (Sandbox Code Playgroud)

postgresql database-design select inheritance

5
推荐指数
1
解决办法
9641
查看次数

标签 统计

database-design ×1

inheritance ×1

postgresql ×1

select ×1