0

This is my query:

select * from (
    select Name, Address
    from table1
    UNION ALL
    select Name, Address
    from table2
) D

When executing this query getting error:

The column 'Name' was specified multiple times for 'D'.

Twix
  • 392
  • 1
  • 12
  • 38
  • Use t1 and t2 as table aliases instead of just integers. – jarlh Jun 30 '15 at 11:41
  • @jarlh this is just an example real table names are different – Twix Jun 30 '15 at 11:42
  • 4
    Your query should not be generating this error. My guess is that the actual query has `*` in the subqueries. – Gordon Linoff Jun 30 '15 at 11:43
  • You can find a complete answer here : http://stackoverflow.com/questions/1058606/sql-2005-the-column-was-specified-multiple-times – Zz Oussama Jun 30 '15 at 11:44
  • @GordonLinoff You are right I am joining multiple tables in subquery, two tables having Name column and I am selecting all columns from one table and Name column from another – Twix Jun 30 '15 at 11:46
  • @ZzOussama I have lots of columns in these tables. I don't write all column names – Twix Jun 30 '15 at 11:50

2 Answers2

2
select * from (
      select t1.Name as t1_Name, t1_Address as t1_Address
          from table t1
             UNION ALL
      select t2.Name as t2_Name, t2_Address as t2_Address
          from table t2
) D

try this

kavetiraviteja
  • 2,058
  • 1
  • 15
  • 35
1

Use alias names

select * from (
    select t1.Name, t1.Address
    from table1 as t1
    UNION ALL
    select t2.Name, t2.Address
    from table2 as t2
) D
Madhivanan
  • 13,470
  • 1
  • 24
  • 29