2017-06-12 104 views
0

我要在选定的模式选择表得到期望的结果是这样PostgreSQL的,获得列名,列类型和描述

column_name | data_type | description 
----------------------------------------- 
id   | integer  | id of bill 
name   | character | 
address  | character | adress of buyer 

注意,有些字段没有说明(列注释)。

现在我只拿到了这个查询,给了我很好的结果,但仅适用于有意见(对于那些在选定表中,并且没有评论在输出中没有代表列)列。

我的查询这对于有评论列返回唯一数据是下一个

SELECT c.column_name, c.data_type, pgd.description 
from pg_catalog.pg_statio_all_tables as st 
inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid) 
inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname) 
where table_schema = 'public' and table_name = 'some_table'; 

如何获得列没有结果有何评论?

+0

只是用'LEFT JOIN '。 'INNER JOIN'给你两个表的交集。如果表A中的行与表B没有关系,则该行将不会包含在输出中。 – KarelG

+0

在发布问题之前尝试使用左连接,结果仍然相同 – KuKeC

回答

1

因为information_schema.columns是肯定的数据表,你引用它不是第一,你需要right outer join代替:

t=# SELECT c.column_name, c.data_type, pgd.description 
from pg_catalog.pg_statio_all_tables as st 
inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid) 
right outer join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname) 
where table_schema = 'public' and table_name = 's150'; 
    column_name | data_type | description 
---------------+-----------+------------- 
customer_name | text  | 
order_id  | text  | 
order_date | date  | 
(3 rows) 
+0

这样做的窍门。谢谢您的帮助 – KuKeC

2

有功能col_description(table_oid, column_number)

select column_name, data_type, col_description('public.my_table'::regclass, ordinal_position) 
from information_schema.columns 
where table_schema = 'public' and table_name = 'my_table'; 

column_name | data_type | col_description 
-------------+-----------+------------------- 
gid   | integer | a comment for gid 
id   | integer | 
max_height | numeric | 
(3 rows)