membersound
membersound

Reputation: 86777

How to reuse alias column in a mysql view?

I want to create a mysql for with a column that references another column that is created within the view.

Example:

CREATE VIEW test AS SELECT '30' AS age, age AS person_age;

This does not work. Is it impossible to reuse a column that has been defined prior?

In real world, my prior age column is the result of a more complex calculation, and I want to reuse that value in an additional column in the view.

Upvotes: 0

Views: 96

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133380

in sql you can or repated the code .. or use a view and select the alias

CREATE VIEW test AS SELECT '30' AS age, '30' AS person_age; 

or use a view and select the alias

CREATE VIEW test AS SELECT '30' AS age; 


CREATE VIEW test2 AS SELECT age, age as person_age from test;

Upvotes: 1

Thorsten Kettner
Thorsten Kettner

Reputation: 94969

You cannot define ageand access it in the same step (this step being your select clause). You must do one thing after the other.

CREATE VIEW test AS 
  SELECT age, age AS person_age
  FROM (SELECT 30 AS age) t;

Upvotes: 1

Related Questions