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
 How to fetch data from two table?

Author  Topic 

vinoth124
Starting Member

4 Posts

Posted - 2008-07-14 : 07:32:56
Hi Friend,

I need your help to resolve this issue...,

I need to fetch data from two tables with some condition...,

For example, I am having two table named as TableA and TableB.

TableA consist of 5 cols Name,Age,Area,Mark,Percentage.
TableB consist of 5 cols lognumber,area,code,district,country

I need to fetch "Area" from TableA where Name='Vinoth'. With refer
to the output(Area) i need to fetch "code" from TableB.

TableA:
Name Age Area Mark Percentage

Vinoth 23 che 88 88
Ram 23 che 88 88
Vinoth 23 mas 88 88
Vinoth 23 kar 88 88
Vinoth 23 tri 88 88


TableB:
lognumber area code district country

1819 che 55 chennai india
1820 mas 56 chennai india
1816 kar 88 karur india

Output Should be :

Area Code
che 55
mas 56
kar 88
tri null


Condition :
1. O/p Area = Select area from tableA where name='Vinoth'
2. 0/p Code = select code from tableB where area="o/p from prev step"



Thanks in advance.







Thanks,
Vinoth R

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2008-07-14 : 07:39:52
A join perhaps?

SELECT < list of columns wanted > 
FROM TableA LEFT OUTER JOIN TableB ON TableA.area = TableB.area
WHERE < desired where conditions >


If you look in Books Online, you'll find a lot of info on joins and how they work. You can also look at the following article for a summary of the join types - [url]http://grounding.co.za/blogs/gail/archive/2008/04/03/joins.aspx[/url]


--
Gail Shaw
SQL Server MVP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-14 : 08:10:34
[code] SELECT Area,Code FROM TableB
WHERE Area IN (SELECT Area FROM TableA WHERE Name='Vinoth')[/code]
Go to Top of Page
   

- Advertisement -