基于列值的动态列别名

时间:2013-05-07 16:01:05

标签: mysql dynamic alias

我想根据mysql中的列值命名列别名。这可能吗?

像这样;

select  
  case when answer_type = 'RB' then 
     answer_type as 'radio'
 else 
     answer_type as 'evrything_else'
 end case
from  XTable 

3 个答案:

答案 0 :(得分:1)

不,这在纯SQL中是不可能的。您可能必须查询数据,然后在执行查询的任何应用程序中处理它。

答案 1 :(得分:1)

select if(answer_type="RB",true,NULL) as radio, 
if(answer_type != ="RB",true,NULL) as everything_else
from XTable;

当你得到一个结果表时你必须拆分它们,其中select的名字或别名是列标签。此方法允许您检查返回的radio(以及everything_else的值)的值。

您要尝试做什么,看看无法使列标题工作:

id | radio MAGICAL OR everytning_else |
1  | RB                               |
2  | NOT_RB                           |

我的方法将为您做什么:

id | radio | everything_else |
1  | true  | NULL            |
2  | NULL  | true            |

答案 2 :(得分:0)

从XTable中选择if(answer_type =“RB”,“radio”,if(answer_type!=“RB”,“everything_else”,NULL))作为answer_type;

相关问题