user2894377
user2894377

Reputation: 1

Creating a column name alias from a select statement

I am trying to create a column alias by looking up a value in another table. This is the MySQL I am trying but keep getting syntax errors - any help would be appreciated.

SELECT
product_code,
bField1 as (select [label_value] from [labels] where [field_value]='bField1'),
bField2 as (select [label_value] from [labels] where [field_value]='bField2'),
....
FROM products

Upvotes: 0

Views: 170

Answers (2)

rollcona
rollcona

Reputation: 163

What you wish is not possible wtih only MySQL.

See below for similar questions:

Dynamic column alias based on column value

Specifying column alias with user-defined variable

I'm not sure why exactly you want to do this but it's always possible to incorporate the alias selection logic with the server side language that is dynamically creating the query. But of course, this would then be a two step process but you are not actually losing out as in your example you are using sub queries anyways.

Upvotes: 1

Hemant Metalia
Hemant Metalia

Reputation: 30638

try as folow:

SELECT
product_code,
(select [label_value] from [labels] where [field_value]='bField1') as bField1,
(select [label_value] from [labels] where [field_value]='bField2') as bField2,
....
FROM products

Upvotes: 2

Related Questions