1

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

像这样的东西;

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

3 回答 3

2

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

于 2013-05-07T16:05:49.437 回答
1
select if(answer_type="RB",true,NULL) as radio, 
if(answer_type != ="RB",true,NULL) as everything_else
from XTable;

当您获得一个结果表时,您必须拆分它们,其中选择的名称或别名是列标签。此方法可让您检查返回的 radio 的值(以及 Everything_else 的值)。

您正在尝试做什么,请参阅无法使列标题起作用:

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

我的方法将为您做什么:

id | radio | everything_else |
1  | true  | NULL            |
2  | NULL  | true            |
于 2013-05-07T16:26:50.400 回答
0

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

于 2015-12-27T21:59:52.207 回答