Akanksha Akanksha - 5 months ago 7
MySQL Question

query to combine the result of two columns into one with alternate values in MYSQL

I need a query to combine the two different columns of the same table in a single column with alternate values.

Table: tblDemo

Id | Name
_________
1 | A
2 | B
3 | C


Required output:

Result
______
1
A
2
B
3
C

Answer

Assuming name is not null, you can use

select coalesce(name, id) as Result from
(
  select id, name from tblDemo
  union all
  select id, null from tblDemo
) as subquery
order by id, name;
Comments