Easy way to alias all columns in a table by adding a prefix or suffix?

From: "D(dot) Dante Lorenso" <dante(at)lorenso(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Easy way to alias all columns in a table by adding a prefix or suffix?
Date: 2008-08-05 01:06:01
Message-ID: 4897A779.7010008@lorenso.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

All,

I have 2 tables with a lot of columns with similar names. I'd like to
join both tables and include all columns from each without naming
collisions. I can use the 'AS' to rename a single column, but is there
a way to do the rename in bulk by just appending a prefix or suffix to
the column names from each respective table?

I want to do something like this:

SELECT a.* AS prefix1_*, b.* AS prefix2_*
FROM a, b
WHERE a.id = b.id
AND a.id = 123;

The result would be to select all columns from "a" but rename each to
have "prefix1_" appended to the front.

a.id -> prefix1_id
b.id -> prefix2_id
a.xpos -> prefix1_xpos
b.xpos -> prefix2_xpos
a.ypos -> prefix1_ypos
b.ypos -> prefix2_ypos

etc...

Does this request make sense? Does something like this exist? I don't
really NEED to have this, I'm just trying to be lazy.

-- Dante

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2008-08-05 01:44:57 Re: Fwd: Returning Cursor
Previous Message Alvaro Herrera 2008-08-04 23:44:17 Re: Initdb problem on debian mips cobalt: Bus error