MySQL: alias column name question
Is it poss开发者_如何学Pythonible 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?
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
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;
精彩评论