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
 select data from 4 tables not linked to each other

Author  Topic 

akki1563
Starting Member

10 Posts

Posted - 2012-02-06 : 07:46:05

Hi,
I have 4 tables Which are independent of each other. But there is one column u_id which is common to all tables which is not primary key nor foreign key. how do i write select statement which should get records where u_id is same in all 4 tables


Please suggest

Regards,
Akki

Mehala
Starting Member

7 Posts

Posted - 2012-02-06 : 08:04:25
U can use union to display all records.


Meks
Go to Top of Page

akki1563
Starting Member

10 Posts

Posted - 2012-02-06 : 08:17:59

Hi Mehala,

Table 1
Table1column1 - Pk
Table1column2
Table3column3
u_id

Table 2
Table2column1 - Pk
Table2column2
Table2column3
u_id

Table 3
Table3column1 - Pk
Table3column2
Table3column3
u_id

Table 4
Table4column1 - Pk
Table4column2
Table4column3
u_id

In the above 4 tables u_id is common column. suppose u_id has value 100 in all 4 tables i need fetch those rows from all 4 tables which
has u_id as 100. How can i do that ?

I tried Select * from Table1 t1, Table2 t2, Table3 t3, Table4 t4
where t1.u_id=t2.u_id=t3_uid
But this is wrong. I am new to sql programming . can u help me out
How to use union on this ?
Regards,
Akki

quote:
Originally posted by Mehala

U can use union to display all records.


Meks

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-06 : 09:21:13
[code]
Select * from
Table1 t1
inner join Table2 t2
on t1.u_id=t2.u_id
inner join Table3 t3
on t3.u_id=t2.u_id
inner join Table4 t4
on t4.u_id=t3.u_id
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-06 : 09:21:45
suggest you to learn concept of joins

http://www.w3schools.com/sql/sql_join.asp

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -