SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 how to select related columns using different tabl
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rajnamm1
Starting Member

India
1 Posts

Posted - 04/07/2013 :  03:20:26  Show Profile  Reply with Quote
In my project I want to select columns from different tables using Tbl1:-

Tbl1 has mem_id,dist_code,city_code
Tbl2 has mem_id,mem_name
Tbl3 has dist_code,dist_name
Tbl4 has city_code,city_name

In Tbl1 either dist_code or city_code will have value >0. Not both will have value >0.

I want to display mem_id,mem_name,dist_name,city_name.

I tried with the following code but coming correct.
Select t.mem_id,m.mem_name,d.dist_name,c.city_name from txfile t join member m on
m.mem_id=t.mem_id join district d on d.dist_code=t.dist_code where t.mem_id=158
join city c on c.city_code=t.city_code where t.mem_id=158

Can anyone help me out?

James K
Flowing Fount of Yak Knowledge

3741 Posts

Posted - 04/07/2013 :  20:58:04  Show Profile  Reply with Quote
The syntax is not quite right. The correct syntax should be as follows.
Select 
	t.mem_id,m.mem_name,d.dist_name,c.city_name 
from 
	txfile t 
	join member m on
		m.mem_id=t.mem_id 
	join district d on 
		d.dist_code=t.dist_code
	join city c on 
		c.city_code=t.city_code
where 
	t.mem_id=158
If you have rows in one table for which there are no corresponding rows in all the other tables, such rows will not be returned via this query. You will need to use one of the outer joins if you do want to return such rows.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000