Tuesday, February 21, 2012

Retrieve data from 2 tables

1st table
Custno nvarchar(9)
CustName nvarchar(50)
with data
Custno CustName
1 kevin
2 john
2nd table
Custno nvarchar(9)
CustName nvarchar(50)
with data
Custno CustName
3 Kelly
4 Julia
5 May
If i want to write a query where the result display all record from 1st
table and 2nd Table like below, what should i write?
1 kevin
2 john
3 Kelly
4 Julia
5 May
my Custno from the 2 tables will never be the same!
when i wrote this
select * from table1, table2
the result as below
1 kevin 3 Kelly
1 kevin 4 Julia
1 kevin 5 May
2 john 3 Kelly
2 john 4 Julia
2 john 5 May
Please help!
--
Message posted via http://www.sqlmonster.comSELECT CustNo, CustName
FROM Table1
UNION ALL
SELECT CustNo, CustName
FROM Table2
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"Gabe Wong via SQLMonster.com" <forum@.SQLMonster.com> wrote in message
news:e2b77d99033b48af971ee828ac6402b5@.SQLMonster.com...
> 1st table
> Custno nvarchar(9)
> CustName nvarchar(50)
> with data
> Custno CustName
> 1 kevin
> 2 john
> 2nd table
> Custno nvarchar(9)
> CustName nvarchar(50)
> with data
> Custno CustName
> 3 Kelly
> 4 Julia
> 5 May
> If i want to write a query where the result display all record from 1st
> table and 2nd Table like below, what should i write?
> 1 kevin
> 2 john
> 3 Kelly
> 4 Julia
> 5 May
> my Custno from the 2 tables will never be the same!
> when i wrote this
> select * from table1, table2
> the result as below
> 1 kevin 3 Kelly
> 1 kevin 4 Julia
> 1 kevin 5 May
> 2 john 3 Kelly
> 2 john 4 Julia
> 2 john 5 May
> Please help!
> --
> Message posted via http://www.sqlmonster.com

No comments:

Post a Comment