How to do a select statement from two or more tables?
-
Hi, What I have is table Student and Poke Student
------------------------------------
Student_Id(PK) | Name1 Alex
2 Bob
3 CavemanPoke
----------------------------------------------------------
Poke_Id(PK) | Poke_Giver_Id(FK) | Poke_Receiver_Id(FK)1 1 2
2 1 2
3 2 1What I'm trying to get is something like this.
----------------------------------------------------
Poke_Id | Poke_Giver_Name | Poke_Receiver_Name1 Alex Bob
2 Alex Bob
3 Bob AlexPlease help.
-
Hi, What I have is table Student and Poke Student
------------------------------------
Student_Id(PK) | Name1 Alex
2 Bob
3 CavemanPoke
----------------------------------------------------------
Poke_Id(PK) | Poke_Giver_Id(FK) | Poke_Receiver_Id(FK)1 1 2
2 1 2
3 2 1What I'm trying to get is something like this.
----------------------------------------------------
Poke_Id | Poke_Giver_Name | Poke_Receiver_Name1 Alex Bob
2 Alex Bob
3 Bob AlexPlease help.
With simple inner join here is query which you need:
select p.poke_id, s.[Name] as Poke_Giver_Name,s2.[Name] as Poke_Receiver_Name
from students s
inner join Poke p on p.Poke_Giver_Id = s.Student_Id
inner join students s2 on s2.Student_Id = p.Poke_Receiver_Id
I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post. www.cacttus.com
-
With simple inner join here is query which you need:
select p.poke_id, s.[Name] as Poke_Giver_Name,s2.[Name] as Poke_Receiver_Name
from students s
inner join Poke p on p.Poke_Giver_Id = s.Student_Id
inner join students s2 on s2.Student_Id = p.Poke_Receiver_Id
I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post. www.cacttus.com
-
Hi, What I have is table Student and Poke Student
------------------------------------
Student_Id(PK) | Name1 Alex
2 Bob
3 CavemanPoke
----------------------------------------------------------
Poke_Id(PK) | Poke_Giver_Id(FK) | Poke_Receiver_Id(FK)1 1 2
2 1 2
3 2 1What I'm trying to get is something like this.
----------------------------------------------------
Poke_Id | Poke_Giver_Name | Poke_Receiver_Name1 Alex Bob
2 Alex Bob
3 Bob AlexPlease help.
Declare @Student table(StudentID int identity,Name varchar(20))
Declare @Poke table(Poke_Id int identity,Poke_Giver_Id int,Poke_Receiver_Id int)
insert into @Student values('Alex'),('Bob'),('Caveman')
insert into @Poke values(1,2),(1,2),(2,1)Select x.Poke_Id,x.Poke_Giver_Name,Poke_Receiver_Name = s.Name
from(
Select p.Poke_Id,s.Name Poke_Giver_Name ,p.Poke_Receiver_Id
from @Poke p
join @Student s
on p.Poke_Giver_Id = s.StudentID
)x
join @Student s on s.StudentID =x.Poke_Receiver_Id/*
Poke_Id Poke_Giver_Name Poke_Receiver_Name
1 Alex Bob
2 Alex Bob
3 Bob Alex
*/Niladri Biswas