user3206236
user3206236

Reputation: 325

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

e.g.

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'

Upvotes: 4

Views: 4991

Answers (4)

Kuberchaun
Kuberchaun

Reputation: 30362

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.

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.

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

Upvotes: 0

Lukas Eklund
Lukas Eklund

Reputation: 6138

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.

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. 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:

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;

Upvotes: 3

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. 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. 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. Sorting by "ordinal_position" will order column names as they appear in the current table definition.

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;

Upvotes: 0

Lajos Veres
Lajos Veres

Reputation: 13725

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'

Upvotes: 0

Related Questions