将头分配给查询mysql

将头分配给查询mysql,mysql,Mysql,我有一个问题 select abc from table1 union select def from table2 我希望将结果作为不同的标题组合在一起 我正在努力 Select * as diff (select abc from table1 union select def from table2) 查询应该是什么,以便将结果聚集在第三列标题diff中 谢谢只需在第一选择中使用标题别名: select abc as diff from table1 union select def

我有一个问题

select abc from table1 union select def from table2
我希望将结果作为不同的标题组合在一起

我正在努力

Select * as diff (select abc from table1 union select def from table2)
查询应该是什么,以便将结果聚集在第三列标题diff中


谢谢

只需在第一选择中使用标题别名:

select abc as diff from table1 union 
select def from table2

如果我理解正确,您希望在外部查询中有一个自定义列名。
您可以通过特别引用要重命名的列来完成此操作。
以下是一个例子:

create table t (c varchar(10));
create table t2 (c2 varchar(10));

insert into t values ('abc');
insert into t2 values ('def');

select s.c as t
from (
    select c 
    from t
  union 
    select c2 
    from t2
) s;
结果:

 t
---
abc
def
有关参考资料,请参阅: