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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Join problem!

SQL Join problem!

Scheduled Pinned Locked Moved Database
helpdatabasequestion
3 Posts 2 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.
  • R Offline
    R Offline
    richiemac
    wrote on last edited by
    #1

    Hi all, Here's the problem... I've got two tables, trains and trainarrivals. Each train has a link to two arrivals records. The first link is to the previous arrival record and the d=second is to the next arrivals record. I'm trying to join the tables using, select * from train LEFT OUTER JOIN trainarrivals ON train.PreviousArrival = trainarrivals.UniqueID OR train.NextArrival = trainarrivals.UniqueID ORDER BY train.UniqueID so that when i get the trains with their arrivals links I will get one row for each train. Due to the fact that the next and previous arrivals are separate records in the trainarrivals table I get two records for each train. My question is, is there any way to do this so I get only one record with both the next and previous arrival data together. Thanks for the help in advance. Rich

    E 1 Reply Last reply
    0
    • R richiemac

      Hi all, Here's the problem... I've got two tables, trains and trainarrivals. Each train has a link to two arrivals records. The first link is to the previous arrival record and the d=second is to the next arrivals record. I'm trying to join the tables using, select * from train LEFT OUTER JOIN trainarrivals ON train.PreviousArrival = trainarrivals.UniqueID OR train.NextArrival = trainarrivals.UniqueID ORDER BY train.UniqueID so that when i get the trains with their arrivals links I will get one row for each train. Due to the fact that the next and previous arrivals are separate records in the trainarrivals table I get two records for each train. My question is, is there any way to do this so I get only one record with both the next and previous arrival data together. Thanks for the help in advance. Rich

      E Offline
      E Offline
      Eric Dahlvang
      wrote on last edited by
      #2

      select * from train
      LEFT OUTER JOIN trainarrivals ta1 ON train.PreviousArrival = ta1.UniqueID
      LEFT OUTER JOIN trainarrivals ta2 ON train.NextArrival = ta2.UniqueID
      ORDER BY train.UniqueID

      --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

      R 1 Reply Last reply
      0
      • E Eric Dahlvang

        select * from train
        LEFT OUTER JOIN trainarrivals ta1 ON train.PreviousArrival = ta1.UniqueID
        LEFT OUTER JOIN trainarrivals ta2 ON train.NextArrival = ta2.UniqueID
        ORDER BY train.UniqueID

        --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

        R Offline
        R Offline
        richiemac
        wrote on last edited by
        #3

        Thanks Eric! It worked a treat. I really didn't know it would be that simple. Thanks again!

        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