开发者

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜