1

Is it possible to alias a column name with the result of a simple SELECT query.

This doesn't work:

SELECT `hlevel1` AS (SELECT `level1` FROM `hierarchy_labels` LIMIT 1) FROM `hierarchy`;

Any Suggestions?

2 Answers 2

3

You can't do this.

Aliases are used to rename a field or to name a calculated field.

If you simply want your results to be named 'hlevel1', you may want to try this:

SELECT level1 as hlevel1 FROM hierarchy_labels LIMIT 1
Sign up to request clarification or add additional context in comments.

Comments

1

Use a prepared statement.

SELECT `level1` INTO @x FROM `hierarchy_labels` LIMIT 1;
SET @s = CONCAT('SELECT `hlevel1` AS `', @x, '` FROM `hierarchy`');
PREPARE s FROM @s;
EXECUTE s;
DEALLOCATE PREPARE s;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.