选择名称与PostgreSQL中的正则表达式匹配的列 [英] Selecting columns whose name matches a regular expression in PostgreSQL
问题描述
如何仅选择名称与PostgreSQL中的正则表达式匹配的列?
How can I select only those columns whose name matches a regular expression in PostgreSQL?
例如,如何仅选择下表中名称以 A开头的列,但未在选择列表中明确枚举?
For example, how do I select only the columns whose name begins with 'A' in the following table, without explicitly enumerating them in the select list?
id A1 A2 A3 A4 A5 B
1 a b c d e f
2 g h i j k l
推荐答案
您将需要编写一个动态sql('从(您的表)中选择'|| colname ||')来完成此操作,并且动态sql应提供以下sql的列名:
You will need to write a dynamic sql('select '||colname||' from (yourtable)') to accomplish this and dynamic sql should have supplied column names from the following sql:
从information_schema.columns中选择column_name,其中table_name =(您的表)和column_name如'a%';
select column_name from information_schema.columns where table_name=(your table) and column_name like 'a%';
这篇关于选择名称与PostgreSQL中的正则表达式匹配的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!