Results 1 to 3 of 3

Thread: Union Query ???

  1. #1
    Join Date
    Apr 2003
    Posts
    2

    Unanswered: Union Query ???

    I have two tables. Table A contains ID, X, Y, Z. Table B contains ID, X, Y, Z, Job_number.
    We recently added Job_number to table B. What I used before was a simple union query to get my resultset. (i.e.
    select * from A
    UNION ALL
    select * from B

    Now that the fields are no longer the same I am unable to use union all. How can I write the query to link the tables in the following manner.
    If table B contains an ID that equals the ID in Table A then show all the information for that record from Table B else if the ID does not exist in Table B then show all the information for the record from Table A.

    Thanks for any input!

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    select B.ID, B.X, B.Y, B.Z, B.Job_Number
    from TableA A
    inner
    join TableB B
    on A.ID = B.ID
    union all
    select ID, X, Y, Z, null
    from TableA
    where not exists
    (select ID from TableB
    where ID = TableA.ID )


    rudy

  3. #3
    Join Date
    Apr 2003
    Posts
    2

    Perfect!

    Works beautiful. Thank You.

    Cheers,
    El Presidente

    Originally posted by r937
    select B.ID, B.X, B.Y, B.Z, B.Job_Number
    from TableA A
    inner
    join TableB B
    on A.ID = B.ID
    union all
    select ID, X, Y, Z, null
    from TableA
    where not exists
    (select ID from TableB
    where ID = TableA.ID )


    rudy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •