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. Is it posible to make several relations between two tables in SQL query ?

Is it posible to make several relations between two tables in SQL query ?

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
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.
  • H Offline
    H Offline
    H R
    wrote on last edited by
    #1

    for example two columns of table1 , have relation to the id column of table2 for example: tbl_city: (id , title) tbl_person: (id , name , CurrentCityID , LastCityID , ...) now , I want to make two relations from tbl_person to tbl_city CurrentCityID and LastCityID must relation to tbl_city this query is wrong: SELECT tbl_person.name , tbl_city.title , tbl_city.title FROM tbl_person JOIN tbl_city ON tbl_person.CurrentCityID = tbl_city.id JOIN tbl_city ON tbl_person.LastCityID = tbl_city.id can you help me ?

    H.R

    P S 2 Replies Last reply
    0
    • H H R

      for example two columns of table1 , have relation to the id column of table2 for example: tbl_city: (id , title) tbl_person: (id , name , CurrentCityID , LastCityID , ...) now , I want to make two relations from tbl_person to tbl_city CurrentCityID and LastCityID must relation to tbl_city this query is wrong: SELECT tbl_person.name , tbl_city.title , tbl_city.title FROM tbl_person JOIN tbl_city ON tbl_person.CurrentCityID = tbl_city.id JOIN tbl_city ON tbl_person.LastCityID = tbl_city.id can you help me ?

      H.R

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      Yes, but you may want an AND instead.

      1 Reply Last reply
      0
      • H H R

        for example two columns of table1 , have relation to the id column of table2 for example: tbl_city: (id , title) tbl_person: (id , name , CurrentCityID , LastCityID , ...) now , I want to make two relations from tbl_person to tbl_city CurrentCityID and LastCityID must relation to tbl_city this query is wrong: SELECT tbl_person.name , tbl_city.title , tbl_city.title FROM tbl_person JOIN tbl_city ON tbl_person.CurrentCityID = tbl_city.id JOIN tbl_city ON tbl_person.LastCityID = tbl_city.id can you help me ?

        H.R

        S Offline
        S Offline
        Stanciu Vlad
        wrote on last edited by
        #3

        Maybe you ar looking for aliases in sql queries? I think this query is the right one for you:

        SELECT
        person.name as personName, currentCity.title as currentCityTitle, lastCity.title as lastCityTitle
        FROM tbl_person as person
        JOIN tbl_city as currentCityON person.CurrentCityID = currentCity.id
        JOIN tbl_city as lastCity ON person.LastCityID = lastCity.id

        Note the as keyword used in the querry.

        I have no smart signature yet...

        P 1 Reply Last reply
        0
        • S Stanciu Vlad

          Maybe you ar looking for aliases in sql queries? I think this query is the right one for you:

          SELECT
          person.name as personName, currentCity.title as currentCityTitle, lastCity.title as lastCityTitle
          FROM tbl_person as person
          JOIN tbl_city as currentCityON person.CurrentCityID = currentCity.id
          JOIN tbl_city as lastCity ON person.LastCityID = lastCity.id

          Note the as keyword used in the querry.

          I have no smart signature yet...

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          Stanciu Vlad wrote:

          Note the as keyword used in the querry.

          I've always left the as out.

          S 1 Reply Last reply
          0
          • P PIEBALDconsult

            Stanciu Vlad wrote:

            Note the as keyword used in the querry.

            I've always left the as out.

            S Offline
            S Offline
            Stanciu Vlad
            wrote on last edited by
            #5

            That is a common practice, but in the previous example I assumed the questioner has no or little knoledge about aliases, therefore I followed a more explicit approach. :)

            I have no smart signature yet...

            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