SELECT with column specified by subquery

From: webcoyote <stein(at)geofusion(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: SELECT with column specified by subquery
Date: 2014-05-05 19:15:26
Message-ID: 1399317326878-5802533.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I desire to create a SELECT statement where one of the column names comes
from another table. Something like:

SELECT id, (SELECT type FROM favorite_food_type WHERE user = 'ralph')
FROM foods;

If Ralph's favorite food type is fruit, ultimately I'd like the query to
become:

SELECT id, fruit FROM foods;

Instead I get:

SELECT id, 'fruit' FROM foods;

Where 'fruit' is a string and not treated as a column name. Is there any
way to do this?

Thank you!

--
View this message in context: http://postgresql.1045698.n5.nabble.com/SELECT-with-column-specified-by-subquery-tp5802533.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G Johnston 2014-05-05 19:29:44 Re: SELECT with column specified by subquery
Previous Message Tim Clarke 2014-05-05 16:40:23 Re: envelope/postage announcement