Value of select column as fieldname

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Value of select column as fieldname

Post by CoderGoblin »

Question:
Can you use the value returned by a select as the "value as column" syntax ?

Example

Code: Select all

value1 | unit1 | value2 | unit2 | value3 | unit3 | value4 | unit4
-------+-------+--------+-------+--------+-------+--------+-------+
     0 |    GB |      1 |    MB |      0 |    kB |     57 | Bytes
to become

Code: Select all

GB   |  MB   |  kB   | Bytes
-------+-------+-------+-------+
     0 |     1 |     0 |    57
If you could I know you would need to use a nested select but I cannot find out how (if it is even possible).

I know I could process each of the columns using php but would prefer to try and simplify it within the db.

I am using POSTGRES.
Post Reply