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
 combining two tables

Author  Topic 

Kotti
Posting Yak Master

129 Posts

Posted - 2009-07-24 : 07:44:19
Hi Friends,

Is there possible to show two tables in a single query.

If i have table A and Table B , i need to show the data of table A and Table B in a single select Query.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-24 : 07:46:38
[code]
select *
from tablea a inner join tableb b on a.pk = b.pk
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Kotti
Posting Yak Master

129 Posts

Posted - 2009-07-24 : 07:52:28
quote:
Originally posted by khtan


select *
from tablea a inner join tableb b on a.pk = b.pk



KH
[spoiler]Time is always against us[/spoiler]





Hi Friend,
Thanks For your Reply.

But i didn't have a common field in both table.

Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-07-24 : 08:51:40
With out common fields you get Cartesian product

select * from temp2 , temp1

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-07-24 : 09:02:06
quote:
Originally posted by Kotti

quote:
Originally posted by khtan


select *
from tablea a inner join tableb b on a.pk = b.pk



KH
[spoiler]Time is always against us[/spoiler]





Hi Friend,
Thanks For your Reply.

But i didn't have a common field in both table.




Post some sample data with expected result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-07-24 : 09:14:06
Hi

Cartesian product is mainly used combination of possible probability right

-------------------------
R..
Go to Top of Page

Kotti
Posting Yak Master

129 Posts

Posted - 2009-07-24 : 09:28:43
Table A
AId AName
1. Kotti
2. Prasad
3. sam
4. Kumar

Table B
BId BName
1. Karthi
2. Nis
3. Sugumar
4. Dhina


I need the output Like this
Name
Kotti
Prasad
sam
Kumar
Karthi
Nis
Sugumar
Dhina

Thanks
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-07-24 : 09:29:18
quote:
Originally posted by rajdaksha

Hi

Cartesian product is mainly used combination of possible probability right

-------------------------
R..



Ya.. It make all the Combination. If both the table have 4 rows then
the result will be 16.

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-07-24 : 09:31:15
quote:
Originally posted by Kotti

Table A
AId AName
1. Kotti
2. Prasad
3. sam
4. Kumar

Table B
BId BName
1. Karthi
2. Nis
3. Sugumar
4. Dhina


I need the output Like this
Name
Kotti
Prasad
sam
Kumar
Karthi
Nis
Sugumar
Dhina

Thanks




Then you use union


Select name from table1 union
Select name from table2

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

Kotti
Posting Yak Master

129 Posts

Posted - 2009-07-24 : 10:35:53
quote:
Originally posted by senthil_nagore

quote:
Originally posted by Kotti

Table A
AId AName
1. Kotti
2. Prasad
3. sam
4. Kumar

Table B
BId BName
1. Karthi
2. Nis
3. Sugumar
4. Dhina


I need the output Like this
Name
Kotti
Prasad
sam
Kumar
Karthi
Nis
Sugumar
Dhina

Thanks




Then you use union


Select name from table1 union
Select name from table2

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/




For sample data only i posted like that.
Table A and B has some more fields.

Table A 3 Fields and Table B 5 fields
Go to Top of Page
   

- Advertisement -