Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecting from two tables with the same columns

Author  Topic 

siasfouche
Starting Member

5 Posts

Posted - 2008-09-04 : 01:25:01
Hi.

For ex.
I have table beers and table spirits looking like

Beers
Name Beer_drink Units
Peter Guiness 3
Peter Castle 2
Mark Guiness 4
Mark Windhoek 1

Spirits
Name Spirit_drink Units
Peter Brandy 2
Peter Gin 1
Mark Wishkey 3
Mark Cane 2

I would now like to combine these tables to look like:
All_Drinks
Name Alcoholic_drink Units
Peter Guiness 3
Peter Castle 2
Peter Brandy 2
Peter Gin 1
Mark Guiness 4
Mark Windhoek 1
Mark Wishkey 3
Mark Cane 2

Can anybody help please.
Thanks

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-09-04 : 01:31:06
Try,

select [Name],Beer_drink AS [Alcoholic_drink],Units from Beers
UNION
select [Name],Spirit_drink AS [Alcoholic_drink],Units from Spirits
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-04 : 01:34:44
since one table contain beer info and other spirits, its reasonable to assume that you wont have duplicates when doing merging. so its better to use UNION ALL as its much better in performance than UNION.
select [Name],Beer_drink AS [Alcoholic_drink],Units from Beers
UNION ALL
select [Name],Spirit_drink AS [Alcoholic_drink],Units from Spirits
Go to Top of Page
   

- Advertisement -