skip to Main Content

I need to select a value from a column in the table as a name of the another column in mysql

for ex ::
select column AS (select column from table where id = 1) from table;

it give me a syntax error .. How can I use select statement inside AS Function

Actually I need to set a value from a column as a name to another column using AS Function in mysql

2

Answers


  1. The answer is simple: It is not possible in SQL. Column aliases are constants.

    Login or Signup to reply.
  2. You can’t do this in a single query. All identifiers must be fixed in the query at the time it is parsed, which is before the query begins reading data. This includes column names and column aliases. The names or aliases of columns cannot be set at runtime based on data read during the query.

    But you can do what you want in two queries.

    1. Query to get the column alias name you want to use. This returns a string.
    2. Use that string as you format the second query. Then the column alias will be fixed in that second query by the time you prepare it.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search