Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to do a select statement from two or more tables?

How to do a select statement from two or more tables?

Scheduled Pinned Locked Moved Database
helptutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eight
    wrote on last edited by
    #1

    Hi, What I have is table Student and Poke Student

    ------------------------------------
    Student_Id(PK) | Name

    1 Alex
    2 Bob
    3 Caveman

    Poke

    ----------------------------------------------------------
    Poke_Id(PK) | Poke_Giver_Id(FK) | Poke_Receiver_Id(FK)

    1 1 2
    2 1 2
    3 2 1

    What I'm trying to get is something like this.

    ----------------------------------------------------
    Poke_Id | Poke_Giver_Name | Poke_Receiver_Name

    1 Alex Bob
    2 Alex Bob
    3 Bob Alex

    Please help.

    B N 2 Replies Last reply
    0
    • E eight

      Hi, What I have is table Student and Poke Student

      ------------------------------------
      Student_Id(PK) | Name

      1 Alex
      2 Bob
      3 Caveman

      Poke

      ----------------------------------------------------------
      Poke_Id(PK) | Poke_Giver_Id(FK) | Poke_Receiver_Id(FK)

      1 1 2
      2 1 2
      3 2 1

      What I'm trying to get is something like this.

      ----------------------------------------------------
      Poke_Id | Poke_Giver_Name | Poke_Receiver_Name

      1 Alex Bob
      2 Alex Bob
      3 Bob Alex

      Please help.

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      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

      E 1 Reply Last reply
      0
      • B Blue_Boy

        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

        E Offline
        E Offline
        eight
        wrote on last edited by
        #3

        Exactly what I need. Thanks :)

        B 1 Reply Last reply
        0
        • E eight

          Exactly what I need. Thanks :)

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          You are welcome.


          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

          1 Reply Last reply
          0
          • E eight

            Hi, What I have is table Student and Poke Student

            ------------------------------------
            Student_Id(PK) | Name

            1 Alex
            2 Bob
            3 Caveman

            Poke

            ----------------------------------------------------------
            Poke_Id(PK) | Poke_Giver_Id(FK) | Poke_Receiver_Id(FK)

            1 1 2
            2 1 2
            3 2 1

            What I'm trying to get is something like this.

            ----------------------------------------------------
            Poke_Id | Poke_Giver_Name | Poke_Receiver_Name

            1 Alex Bob
            2 Alex Bob
            3 Bob Alex

            Please help.

            N Offline
            N Offline
            Niladri_Biswas
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups