SELECT语句postgres中的动态列 [英] Dynamic column in SELECT statement postgres
本文介绍了SELECT语句postgres中的动态列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Postgresql还是很陌生。
I am quite new to the postgresql.
实现此目标的最佳方法是什么?
what is the best way to achieve this?
SELECT get_columns()
FROM table_name;
get_columns()
将提供列名用于查询。我看到有人建议使用EXECUTE语句,但我无法正常工作。
get_columns()
will provide the column names for the query. I saw people advising to use EXECUTE statement but I couldn't got that working.
我们说有一个表Test,列a,b,c,
和我想运行
Lets say there is table Test with columns a,b,c and I want to run
SELECT a,b FROM Test;
SELECT a,c FROM Test;
具有动态生成的列名。
推荐答案
要编写动态查询,您必须执行以下操作:
In order to write a dynamic query you would have to do something like:
EXECUTE 'SELECT '|| get_columns()|| ' FROM table_name' INTO results
请阅读文档:
http://developer.postgresql.org/pgdocs/postgres/plpgsql-statements.html
这篇关于SELECT语句postgres中的动态列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文