Ank
Ank

Reputation: 6270

Assign header to a query mysql

I have a query

select abc from table1 union select def from table2

I want the results to be clubbed together as a different header.

I am trying

Select * as diff (select abc from table1 union select def from table2)

What should be the query so that the result is clubbed in a third column header diff.

Thanks

Upvotes: 1

Views: 231

Answers (2)

Vikram
Vikram

Reputation: 8333

just use the header alias in first select:

select abc as diff from table1 union 
select def from table2

Upvotes: 1

mechanical_meat
mechanical_meat

Reputation: 169334

If I understand correctly, you would like to have a custom column name in the outer query.
You can do that by specifically referencing the column which you would like to rename.
Here is an example:

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;

Results in:

 t
---
abc
def

For reference see: http://sqlize.com/7RKZw41Hqx

Upvotes: 1

Related Questions