我想在选定的模式中选择所需的结果,如此
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 :(得分:4)
有功能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)
答案 1 :(得分:2)
因为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)
答案 2 :(得分:1)
在 col_description() 上使用 pg_attribute:
select
attname as column_name,
atttypid::regtype as data_type,
col_description(attrelid, attnum) as description
from pg_attribute
where attrelid = '(myschema.)mytable'::regclass
and attnum > 0 -- hide system columns
and not attisdropped -- hide dead/dropped columns
order by attnum;