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 select from three tables

Author  Topic 

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-11-17 : 02:25:46
Hi all..
My tables are here

Table 1

user_id(primary_key)....name
1.....................wilson
2....................jafry
3...................xxxxxx
4...................yyyyyy
.
.
100
Table2

Received_id(primary_key)....user_id
10............................1
11............................2

Table 3
id(primary_key).......Received_id.....user_id
1.........................10...............2
2.........................10..............3
3.........................11..............1

Now i need my output like this

user_id(primary_key)....name
1.....wilson
2.....jafry
3......xxxxx

I only have Received_id is 10.

How to get the output.. Pls help me !

chadmat
The Chadinator

1974 Posts

Posted - 2010-11-17 : 03:14:09
[code]
SELECT a.User_Id, name
FROM Table1 a
JOIN Table2 b ON a.User_Id=b.User_Id
WHERE Received_Id = 10

[/code]


-Chad
Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-11-17 : 03:24:26
Tnx chadmat For your reply...

I need to select from table 2 & 3 ..Finally from table one for the name
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2010-11-17 : 03:28:07
What do you need from Table3?

User_id and name are both in Table1, and you want to filter by Received_id which is in Table2. There is no need to involve table3 unless you need something from that table.

-Chad
Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-11-17 : 03:56:05
Please see my table 3 ..
Table 3
id(primary_key).......Received_id.....user_id
1.........................10...............2
2.........................10..............3
3.........................11..............1

It also contains one user_id for the received_id..
This row
2.........................10..............3

from the second table we can't get this value
Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-11-17 : 03:58:33
Table 2 & table 3 contains some list of user_id for the given received_id ..
We need to select that user_id s..
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-11-17 : 04:07:04
Then Replace Table2 with Table3 in chadmat's Query

Vaibhav T

If I cant go back, I want to go fast...
Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-11-17 : 04:48:57
@vaibhavktiwari83 I need to combine both the tables .. table2 and table 3 contains different datas.. after joining these tables need to select the names from table1 .. Pls ask me i you need more info.
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-11-17 : 05:09:39
Try this -


SELECT A.UserID, B.Name FROM
(
SELECT Distinct a.User_Id
FROM Table2 a
JOIN Table3 b ON a.Received_id = b.Received_id
WHERE Received_Id = 10
) A
INNER JOIN Table1 B ON A.User_Id = B.User_Id


Vaibhav T

If I cant go back, I want to go fast...
Go to Top of Page
   

- Advertisement -