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. Union query.. I guess:)

Union query.. I guess:)

Scheduled Pinned Locked Moved Database
databaseregexquestion
7 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.
  • M Offline
    M Offline
    Muammar
    wrote on last edited by
    #1

    SELECT * FROM CITY_POPULATION; CITY_ID       CITY_POPULATION -------           --------------- 1                 100 2                 200 3                 300 SELECT * FROM CITY_INFO; CITY_ID      CITY_NAME -------          --------- 1                 FIRST CITY 2                 SECOND CITY 3                 THIRD CITY How can I have some sort of union query that would have the city id, name and population in one output where the id columns from the two tables match, I mean, the city names from the second table should be picked up to match their id number from the other table showing how much population is in that country id with it's name.. something like: ID, Country Name, Population Many thanks guys!

    modified on Wednesday, September 3, 2008 3:30 PM

    T W 2 Replies Last reply
    0
    • M Muammar

      SELECT * FROM CITY_POPULATION; CITY_ID       CITY_POPULATION -------           --------------- 1                 100 2                 200 3                 300 SELECT * FROM CITY_INFO; CITY_ID      CITY_NAME -------          --------- 1                 FIRST CITY 2                 SECOND CITY 3                 THIRD CITY How can I have some sort of union query that would have the city id, name and population in one output where the id columns from the two tables match, I mean, the city names from the second table should be picked up to match their id number from the other table showing how much population is in that country id with it's name.. something like: ID, Country Name, Population Many thanks guys!

      modified on Wednesday, September 3, 2008 3:30 PM

      T Offline
      T Offline
      TheFM234
      wrote on last edited by
      #2

      You would use a join for this. Select * From City_Info i Inner Join City_Population p on p.City_Id = i.City_Id The output should be similar to:

      City_Id City_Name City_Id City_Population


      1 First City 1 100
      2 Second City 2 200
      3 Third City 3 300

      You probably wouldn't want City_Id two times, so you would need to specify the columns that you want in the select list. Be sure to qualify the column names so you don't get an ambiguous column error (ex: i.City_id, i.City_Name, p.City_Population).

      M 2 Replies Last reply
      0
      • M Muammar

        SELECT * FROM CITY_POPULATION; CITY_ID       CITY_POPULATION -------           --------------- 1                 100 2                 200 3                 300 SELECT * FROM CITY_INFO; CITY_ID      CITY_NAME -------          --------- 1                 FIRST CITY 2                 SECOND CITY 3                 THIRD CITY How can I have some sort of union query that would have the city id, name and population in one output where the id columns from the two tables match, I mean, the city names from the second table should be picked up to match their id number from the other table showing how much population is in that country id with it's name.. something like: ID, Country Name, Population Many thanks guys!

        modified on Wednesday, September 3, 2008 3:30 PM

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        Do you really want to union or perhaps join? Do you want output like :

        City_Id City_Population CityName

        1 100 First City
        2 200 Second City...

        or something else?

        M 1 Reply Last reply
        0
        • T TheFM234

          You would use a join for this. Select * From City_Info i Inner Join City_Population p on p.City_Id = i.City_Id The output should be similar to:

          City_Id City_Name City_Id City_Population


          1 First City 1 100
          2 Second City 2 200
          3 Third City 3 300

          You probably wouldn't want City_Id two times, so you would need to specify the columns that you want in the select list. Be sure to qualify the column names so you don't get an ambiguous column error (ex: i.City_id, i.City_Name, p.City_Population).

          M Offline
          M Offline
          Muammar
          wrote on last edited by
          #4

          Thank you:) Just what I was looking for!


          All generalizations are wrong, including this one! (\ /) (O.o) (><)

          1 Reply Last reply
          0
          • W Wendelius

            Do you really want to union or perhaps join? Do you want output like :

            City_Id City_Population CityName

            1 100 First City
            2 200 Second City...

            or something else?

            M Offline
            M Offline
            Muammar
            wrote on last edited by
            #5

            Mika Wendelius wrote:

            union or perhaps join?

            :-O Thanks for the hint mate!


            All generalizations are wrong, including this one! (\ /) (O.o) (><)

            W 1 Reply Last reply
            0
            • M Muammar

              Mika Wendelius wrote:

              union or perhaps join?

              :-O Thanks for the hint mate!


              All generalizations are wrong, including this one! (\ /) (O.o) (><)

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              No problem Seems that Scott has faster (and more describing) fingers than I :-D

              1 Reply Last reply
              0
              • T TheFM234

                You would use a join for this. Select * From City_Info i Inner Join City_Population p on p.City_Id = i.City_Id The output should be similar to:

                City_Id City_Name City_Id City_Population


                1 First City 1 100
                2 Second City 2 200
                3 Third City 3 300

                You probably wouldn't want City_Id two times, so you would need to specify the columns that you want in the select list. Be sure to qualify the column names so you don't get an ambiguous column error (ex: i.City_id, i.City_Name, p.City_Population).

                M Offline
                M Offline
                Muammar
                wrote on last edited by
                #7

                Sorry Scott, but I still have a question though Using your inner join statement, why do I get the same value from any column of the second table, like population here from city population would be 100 for the whole column:confused: Please help me Mr. "Scott Tiger ;)"


                All generalizations are wrong, including this one! (\ /) (O.o) (><)

                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