简体   繁体   English

查询中的列名

[英]Column names from query

I'm trying to get make a query to return a list of the column names from a query result in postgresql 我正在尝试进行查询以从PostgreSQL的查询结果中返回列名称的列表

eg 例如

SELECT column_name 
  FROM ( 
        SELECT table1.* 
          FROM table1 
               LEFT JOIN table2 
                         ON table1.id = table2.tbl1_id
        )

is this possible 这可能吗

I DO NOT WANT THE COLUMNS FROM A SINGULAR TABLE!!! 我不希望单张桌子上的列!!! so please dont tell me to do 所以请不要告诉我去做

SELECT column_name
  FROM information_schema.columns
 WHERE table_name = 'table1'

Querying tables returns data. 查询表返回数据。 Querying the information schema returns metadata. 查询信息模式将返回元数据。 Column names are metadata. 列名称是元数据。

SELECT 'table1', column_name
  FROM information_schema.columns
 WHERE table_catalog = 'database_name'
   AND table_schema  = 'schema_name'
   AND table_name = 'table1'
UNION ALL
SELECT 'table2', column_name
  FROM information_schema.columns
 WHERE table_catalog = 'database_name'
   AND table_schema  = 'schema_name'
   AND table_name = 'table2';

"schema_name" is probably public . “ schema_name”可能是公共的 But be warned that you can have multiple tables with the same name, as long as they are in different schemas. 但请注意,只要它们位于不同的架构中,就可以有多个具有相同名称的表。 (You could have "public.table1" and "private.table1".) You can also have multiple tables with the same name in different databases. (您可以具有“ public.table1”和“ private.table1”。)您还可以在不同的数据库中拥有多个具有相同名称的表。 You need to specify all three values--database name, schema name, table name--to be sure of getting the right data. 您需要指定所有三个值-数据库名称,架构名称,表名称-以确保获取正确的数据。

If I had to do this in production I'd probably output more data. 如果必须在生产中执行此操作,则可能会输出更多数据。 I'd also probably include the column "ordinal_position", and sort on it. 我可能还会包括“ ordinal_position”列,并对其进行排序。 Sorting by "ordinal_position" will order column names as they appear in the current table definition. 按“ ordinal_position”排序将对列名进行排序,就像它们在当前表定义中一样。

SELECT table_catalog
     , table_schema
     , table_name 
     , column_name
     , ordinal_position
FROM information_schema.columns
WHERE table_catalog = 'sandbox'
  AND table_schema  = 'public'
  AND table_name IN ('table1', 'table2')
ORDER BY table_catalog
       , table_schema
       , table_name
       , ordinal_position;

Maybe there is a less hacky way, but I think this should work: 也许有一种更简单的方法,但是我认为这应该可行:

create table query_column_sample from (your query where 1=2);
SELECT column_name
  FROM information_schema.columns
 WHERE table_name = 'query_column_sample'

The only reasonable approach I can come up with is to do something like this. 我能想到的唯一合理的方法是做这样的事情。 Schema location plays into this, but I'm just posting the basic idea. 模式位置参与其中,但我只是发布基本概念。 SQL doesn't have reflection at run time unless you go and hit meta data tables, which is what you seem to be trying to achieve here. 除非您去命中元数据表,否则SQL在运行时不会进行反射,这似乎是您在此处试图实现的目标。

SELECT table1.* 
INTO x
      FROM table1 
           LEFT JOIN table2 
                     ON table1.id = table2.tbl1_id LIMIT 0;

select *
from information_schema.columns
where table_Name = 'x';

Also interesting enough while traveling the Internets over lunch I ran into this. 在午餐时间上网时也很有趣,我遇到了这个。 Works with PostgreSQL 8.2.0 or higher. 适用于PostgreSQL 8.2.0或更高版本。

https://github.com/theory/colnames/tree/master/test/sql https://github.com/theory/colnames/tree/master/test/sql

I don't know why you would ever need to do this, but it is possible using some of the JSON functions introduced in 9.3. 我不知道为什么您需要这样做,但是可以使用9.3中引入的一些JSON函数。

SELECT json_object_keys(row_to_json(t)) FROM
 (SELECT * FROM table1
  LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1) t;

This will give you the name of every column returned for a single row. 这将为您返回单行返回的每一列的名称。 Without the LIMIT you would get the columns repeated for every row returned. 如果没有LIMIT您将为返回的每一行重复列。 If you wanted to see the values returned as well you can get more complex: 如果您还想查看返回的值,则可以变得更加复杂:

WITH t as
  (SELECT * FROM table1
   LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1)
SELECT json_data.key, json_data.value
FROM t, json_each_text(row_to_json(t)) AS json_data;

Both these queries will return all the columns even if they are named the same. 这两个查询都将返回所有列,即使它们的名称相同。 If all you want is a list of unique column names, you can utilize hstore: 如果只需要一个唯一的列名列表,则可以使用hstore:

CREATE EXTENSION hstore; --Create the extension if you need it.

SELECT akeys(hstore(t)) as array_of_columns
FROM
(SELECT * FROM table1
 LEFT JOIN table2 ON table1.id = table2.tbl1id LIMIT 1) t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM