SQL exclude a column using SELECT * [except columnA] FROM tableA?

后端 未结 30 2551
花落未央
花落未央 2020-11-21 23:15

We all know that to select all columns from a table, we can use

SELECT * FROM tableA

Is there a way to exclude column(s) from a table witho

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 00:04

    Postgres sql has a way of doing it

    pls refer: http://www.postgresonline.com/journal/archives/41-How-to-SELECT-ALL-EXCEPT-some-columns-in-a-table.html

    The Information Schema Hack Way

    SELECT 'SELECT ' || array_to_string(ARRAY(SELECT 'o' || '.' || c.column_name
            FROM information_schema.columns As c
                WHERE table_name = 'officepark' 
                AND  c.column_name NOT IN('officeparkid', 'contractor')
        ), ',') || ' FROM officepark As o' As sqlstmt
    

    The above for my particular example table - generates an sql statement that looks like this

    SELECT o.officepark,o.owner,o.squarefootage FROM officepark As o

提交回复
热议问题